草庐IT

non-constexpr

全部标签

c++ - 在 mac os x 中制作期间出现错误未知类型名称 'constexpr'

当我使用scons编写一些程序时,会发生类似这样的错误,error:unknowntypename'constexpr'error:expectedunqualified-id我已经安装了最新版本的xcode和xquartz。这是我的macclang版本AppleLLVMversion8.1.0(clang-802.0.42)Target:x86_64-apple-darwin16.6.0Threadmodel:posixInstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xct

c++ - 编译时数学函数的 constexpr vs 模板?

我对C++2011的新关键字constexpr很困惑。我想知道在编写编译时函数(尤其是数学函数)时在哪里使用constexpr以及在哪里使用模板元编程。例如,如果我们采用整数pow函数://1:templateinlinedoubletpow(doublex){returnx*tpow(x);}templateinlinedoubletpow(doublex){return1.0;}//2:constexprdoublecpow(doublex,intN){return(N>0)?(x*cpow(x,N-1)):(1.0);}//3:templateconstexprdoubletcp

c++ - libc++ 与 VC++ : Can non-UTF conversions be done with wstring_convert?

C++11的std::wstring_convert效果很好*用于标准UTF-8UTF-16/UCS2/UCS4转换。但是,当我尝试使用不是来自的构面实例化wstring_convert或wbuffer_convert时,它没有按预期工作://worksasexpectedstd::wstring_convert>ucs4conv;//Now,byanalogy,Iwanttotrythis:std::wstring_convert>gbconv(newstd::codecvt_byname("zh_CN.gb18030"));Clang++错误提示“在~wstring_convert

c++ - 使用引用的 constexpr 静态成员作为模板参数

我想弄清楚GCC或Clang对C++17标准的解释是否不同/错误。这是我的代码,它使用GCC8编译,但不使用Clang6:structBoolHolder{constexprstaticboolb=true;};templateclassFoo{};intmain(){BoolHolderb;Foof;//WorksBoolHolder&br=b;Foof2;//Doesn'twork}我想知道这是为什么。显然,b.b是一个有效的constexpr(或者第一个Foo是无效的)。是br.b不是有效的constexpr?为什么?对象或引用本身应该与它无关,因为我们在这里访问静态conste

c++ - 隐式转换 : const reference vs non-const reference vs non-reference

考虑这段代码,structA{};structB{B(constA&){}};voidf(B){cout这compilesfine,运行良好。但是,如果我将f(B)更改为f(B&),它doesn'tcompile.如果我写f(constB&),它又是compilesfine,运行良好。原因和道理是什么?总结:voidf(B);//okayvoidf(B&);//errorvoidf(constB&);//okay对于每种情况,我想听听语言规范中的原因、基本原理和引用资料。当然,函数签名本身并没有错。A隐式转换为B和constB&,但不会转换为B&,这会导致编译错误。

c++ - 将静态 constexpr 类成员分配给运行时变量

我知道有很多类似的问题,但不知何故不同的问题。这是关于以下情况:#include#includetemplateclassMyClass{public:staticconstexprstd::arrayARRAY{{4,3,1,5}};};intmain(){constexprstd::arraymy_array(MyClass::ARRAY);//worksfine->canusetheARRAYtoinitializeconstexprstd::arrayconstexprintVALUE=5*MyClass::ARRAY[0];//worksalsofineintvalue;va

c++ - 用 constexpr 迭代

我想写这样的东西:templatevoidf(){}for(constexprinti:{1,2,3}){f();}是否可以迭代constexpr?谢谢 最佳答案 正如您可能理解的那样,您不能执行以下操作:for(constexprinti:{1,2,3}){f();}因为,如果i是在循环中从1到3变化,那么它是一个变量并且不是编译时常量。并且变量不能是模板参数,如f:只有编译时常量可以作为模板参数。在C++11及更高版本中,感谢variadictemplates,您可以有效地迭代编译时常量的任意序列通过使用接受合适的任意序列的模板

c++ - 强制编译时 constexpr

这个问题在这里已经有了答案:Howtoensureconstexprfunctionnevercalledatruntime?(5个答案)关闭12个月前。在C++11中,我们得到constexpr:constexprintfoo(intx){returnx+1;}是否可以使用x的动态值调用foo编译时错误?也就是说,我想创建一个只能传入constexpr参数的foo。

c++ - 为什么 constexpr 必须是静态的?

尝试创建具有constexpr属性但不是静态的结构成员会导致编译器错误(见下文)。这是为什么?对于单个常量值,我会在程序终止之前在内存中保留这个值,而不仅仅是结构的范围吗?我应该重新使用宏吗?structfoo{constexprintn=10;//...};error:non-staticdatamembercannotbeconstexpr;didyouintendtomakeitstatic? 最佳答案 不知道官方的合理性。但肯定会导致困惑。一方面,我看不出非静态数据成员是constexpr意味着什么。您能做到以下几点吗?st

c++ - 为什么 `std::invoke` 不是 constexpr?

不应该std::invoke成为constexpr尤其是在constexprlambdasinC++17之后?是否有任何障碍可以阻止这种情况发生? 最佳答案 更新:P1065将使它成为constexpr。由于历史原因保留原帖:来自theproposal:Althoughthereispossibilitytoimplementstandardconforminginvokefunctiontemplateasaconstexprfunction,theproposedwordingdoesnotrequiresuchimplemen