草庐IT

ConstexPR

全部标签

c++ - 带有 constexpr 的编译时命名参数习语

我最近遇到了很多情况,其中命名参数习语很有用,但我希望它在编译时得到保证。在链中返回引用的标准方法似乎总是调用运行时构造函数(使用Clang3.3-O3编译)。我无法找到与此相关的任何内容,所以我试图让它与constexpr一起工作并获得一些功能:classFoo{private:int_a;int_b;public:constexprFoo():_a(0),_b(0){}constexprFoo(inta,intb):_a(a),_b(b){}constexprFoo(constFoo&other):_a(other._a),_b(other._b){}constexprFooSet

c++ - 使用带有静态 constexpr 的模板类时如何修复链接错误?

我有以下代码#includetemplateclassA{public:staticconstexprintarr[5]={1,2,3,4,5};};templateconstexprintA::arr[5];intmain(){Aa;std::cout编译顺利,但我有一个我不明白的链接错误g++-std=c++11test.cpp-otest/tmp/ccFL19bt.o:Infunction`main':test01.cpp:(.text+0xa):undefinedreferenceto`A::arr'collect2:error:ldreturned1exitstatus

c++ - 在 constexpr 中使用 strcmp 的编译器差异

以下在GCC中编译但不在Clang中编译:#includeconstexprinttest=strcmp("test","test");所以我的问题是,GCC如何以不同方式处理strcmp以使其成为可能?strcmp是某种类型的内置函数,还是它的标准库具有包含constexpr的strcmp的非标准定义? 最佳答案 代码在gcc上编译,因为它提供了一个built-inversion在编译时评估的strcmp,假设您将字符串文字传递给函数。gcc将rejectthecode如果您传递-fno-builtin(或-fno-builtin

c++ - 在编译时将 initializer_list<T> 转换为 initializer_list<vector<T>>

我有一个类构造函数接受一个initializer_list这个构造函数必须运行接受一个的父类构造函数initializer_list>.所以我必须将初始化列表转换为二维初始化列表。{1,2,3,4}to{{1},{2},{3},{4}}编辑:我有一个类构造函数接受一个initializer_list这个构造函数必须运行接受一个的父类构造函数initializer_list>.所以我必须将初始化列表转换为二维初始化列表。{1,2,3,4}to{{1},{2},{3},{4}} 最佳答案 为什么不让您的子类获取参数包并将其转发给父构造函

c++ 11通过constexpr在编译时获取字符串长度

#includeconstexprsize_tconstLength(constchar*str){return(*str==0)?0:constLength(str+1)+1;}int_tmain(intargc,_TCHAR*argv[]){constchar*p="1234567";size_ti=constLength(p);printf(p);printf("%d",i);return0;}大家好我想在编译时获取字符串的长度。所以我写了上面的代码。但是在反汇编代码中,我发现下面名为sub_401000的“constLength”函数将导致计算字符串长度的运行时开销。是否有有问

c++ - N4140 的要点 §5.19/2.3 中的单词 "undefined"是什么意思?

来自N4140§5.19/2.3(强调我的)—aninvocationofanundefinedconstexprfunctionoranundefinedconstexprconstructor;从§7.1.5/2开始,constexpr函数和构造函数是隐式内联的,也就是说,如果constexpr函数未在TU中定义,则代码将不会编译。 最佳答案 此项目符号由defectreport699添加并且它要求必须在使用前定义一个constexpr函数或构造函数。缺陷报告将以下示例添加到7.1.5以演示规则:constexprintsqua

c++ - 理解左值到右值转换的例子

我很难理解这段代码(来自C++14标准草案[conv.lval]的示例)如何调用g(false)的未定义行为>。为什么constexpr使程序有效?此外,“不访问y.n”是什么意思?在对g()的两次调用中,我们都返回了n数据成员,那么为什么最后一行说它不访问它呢?structS{intn;};autof(){Sx{1};constexprSy{2};return[&](boolb){return(b?y:x).n;};}autog=f();intm=g(false);//undefinedbehaviorduetoaccessofx.noutsideits//lifetimeintn=

c++ - 对于内联函数和 constexpr 函数, "obey ODR"是什么意思?

我刚读到constexpr和inline函数遵循一个定义规则,但它们的定义必须相同。所以我试了一下:inlinevoidfoo(){return;}inlinevoidfoo(){return;}intmain(){foo();};错误:'voidfoo()'的重新定义,和constexprintfoo(){return1;}constexprintfoo(){return1;}intmain(){constexprx=foo();};错误:'constexprintfoo()'的重新定义那么究竟是什么意思,constexpr和inline函数可以服从ODR?

c++ - static_casting a constexpr void* 的结果是常量表达式吗?

clang正在拒绝gcc允许的这段代码:intmain(){staticconstexprconstvoid*vp=nullptr;staticconstexprconstchar*cp=static_cast(vp);}具有以下内容:error:constexprvariable'cp'mustbeinitializedbyaconstantexpressionstaticconstexprconstchar*cp=static_cast(vp);阅读完N3797中的最终list后5.9/2我没有看到任何禁止在常量表达式中使用static_cast的内容。我是在看错地方还是误读了什么

c++ - 没有优化的 g++ 4.9 上静态 constexpr 的 undefined reference

我有以下代码:#include#includeusingnamespacestd::chrono_literals;#defineMSG"hello"#defineDUR1000msclassmwe{public:staticconstexprautomsg=MSG;staticconstexprautodur_1=DUR;staticconstexprstd::chrono::millisecondsdur_2=DUR;staticconststd::chrono::millisecondsdur_3;staticconstexprdecltype(DUR)dur_4=DUR;};c