草庐IT

static_assert

全部标签

c++ - 警告 : ISO C++ forbids converting a string constant to ‘char*’ for a static `constexpr char*` data member

这个问题在这里已经有了答案:constexprconstvsconstexprvariables?(3个回答)关闭3年前。为什么这段代码会返回警告warning:ISOC++forbidsconvertingastringconstantto‘char*’[-Wwrite-strings]如果Aconstexprspecifierusedinanobjectdeclarationornon-staticmemberfunction(untilC++14)impliesconst.Aconstexprspecifierusedinafunctionorstaticmembervariab

c++ - 即使模板函数在任何地方都没有调用,static_assert 也无法编译

我使用带有标志c++0x的g++4.6.3(当前是ubuntu12.04的默认包),我偶然发现了这个:templateinlineTgetValue(AnObject&){static_assert(false,"thisfunctionhastobeimplementedfordesiredtype");}编译错误:static_assertionfailed"thisfunctionhastobeimplementedforthedesiredtype"即使我还没有在任何地方调用此函数。这是一个g++错误吗?仅当在代码中的某处调用此函数时才应实例化此函数。

c++ - assert 和 static_assert 有什么区别?

我知道static_assert在编译时进行断言,而assert在运行时进行断言,但实际上有什么区别?据我了解,在内心深处它们是代码片段,例如if(condition==false)exit();谁能给我一个例子,说明onlystatic_assert或onlyassert会在哪里工作?他们能做一些简单的if语句做不到的事情吗?使用它们是不好的做法吗? 最佳答案 你问了三个问题,所以我会试着回答每个问题。谁能给我一个例子,说明onlystatic_assert或onlyassert可以在哪里工作?static_assert非常适合在

c++ - static const 与 const static

我不明白我的C++类中这两个语句之间的区别:classMyClass{public:private:staticconstintvar=0;//Option1conststaticintvar=0;//Option2};b/w选项1和选项2有什么区别??他们都编译。 最佳答案 它们的意思完全一样。您可以自由选择您认为更容易阅读的内容。在C中,您应该将static放在开头,但这还不是必需的。我不确定C++在这方面是否遵循C。6.11.5Storage-classspecifiers1Theplacementofastorage-cla

c++ - 在发布版本中使用 assert() 时避免未使用的变量警告

有时局部变量仅用于在assert()中检查它,就像这样-intResult=Func();assert(Result==1);在Release版本中编译代码时,assert()s通常被禁用,因此此代码可能会产生关于Result已设置但从未读取的警告。一种可能的解决方法是-intResult=Func();if(Result==1){assert(0);}但是它需要输入太多,不利于眼睛并且导致总是检查条件(是的,编译器可能会优化检查,但仍然如此)。我正在寻找一种替代方法来表达这个assert()以一种不会导致警告的方式,但仍然易于使用并避免更改assert()的语义。(在此代码区域中使用

c++ - 在 static_assert 输出中集成类型名称?

我喜欢提供有用的错误/消息,我也想为我的static_assert这样做s。问题是,它们依赖于模板参数。通常,由于引发的错误,这些参数将在途中或其他地方显示,但它们要么是模糊的,要么没有分组,因此它们是有意义的。示例:templatestructfake_dependency{staticboolconstvalue=false;};templatestructFoo{Foo(){}templateFoo(Fooconst&){static_assert(fake_dependency::value,"CannotcreateFoofromFoo.");}};intmain(){Foo

c++ - 模板元编程 - 使用 Enum Hack 和 Static Const 的区别

我想知道在使用模板元编程技术时使用静态const和enumhack有什么区别。EX:(通过TMP斐波那契)templatestructTMPFib{staticconstintval=TMPFib::val+TMPFib::val;};templatestructTMPFib{staticconstintval=1;};templatestructTMPFib{staticconstintval=0;};对比templatestructTMPFib{enum{val=TMPFib::val+TMPFib::val};};templatestructTMPFib{enum{val=1};

c++ - g++ 链接器 : force static linking if static library exists?

我有一个链接到许多库的程序。g++,默认情况下,更喜欢链接到共享库,即使相应的存档存在。如果存在静态存档,我如何更改此首选项以首选静态存档而不是动态库?​​注意,我使用了-static选项,但它会尝试为所有库查找静态存档,这不是我想要的。 最佳答案 g++-Wl,-Bstatic-lz-lfoo-Wl,-Bdynamic-lbar-Wl,--as-needed将zlib和libfoo链接为static,并将libbar链接为dynamic。--as-needed将丢弃任何未使用的动态库。

javascript - Chai 中的 “assert” 、 “expect” 和 “should” 有什么区别?

assert、expect和should有什么区别?什么时候用什么?assert.equal(3,'3','==coercesvaluestostrings');varfoo='bar';expect(foo).to.equal('bar');foo.should.equal('bar'); 最佳答案 区别是documentedthere.这三个接口(interface)呈现不同风格的执行断言。最终,他们执行相同的任务。一些用户更喜欢一种风格而不是另一种风格。话虽如此,还有一些技术考虑值得强调:assert和expect接口(int

go - 这些 Go 构建标志是什么意思? netgo -extldflags "-lm -lstdc++ -static"'

我目前正在学习微服务在线类(class),我将小型Go应用程序部署到docker容器。用于构建二进制文件的长而丑陋的命令行是这样的:gobuild--tagsnetgo--ldflags'-extldflags"-lm-lstdc++-static"'到目前为止,我只是使用goinstall来编译我的go应用程序。谁能给我解释一下这个命令? 最佳答案 --标签netgoisusedtousegolangnetworkstack--ldflagssetstheflagsthatarepassedto'gotoollink'ldflag