草庐IT

constexpr-ness

全部标签

c++ - constexpr if 和 static_assert

P0292R1constexprif已included,在C++17的轨道上。它看起来很有用(并且可以代替SFINAE的使用),但是关于static_assert的评论格式错误,不需要诊断在false分支中让我感到害怕:Disarmingstatic_assertdeclarationsinthenon-takenbranchofaconstexprifisnotproposed.voidf(){ifconstexpr(false)static_assert(false);//ill-formed}templatevoidg(){ifconstexpr(false)static_ass

c++ - 如何断言 constexpr if else 子句永远不会发生?

如果条件为真,我想在非constexpr时引发编译时错误,例如:ifconstexpr(condition1){...}elseifconstexpr(condition2){....}elseifconstexpr(condition3){....}else{//Iwanttheelseclausenevertaken.ButIheardthecodebelowisnotallowedstatic_assert(false);}//I'drathernotrepeattheconditionsagainlikethis:static_assert(condition1||condit

c++ - 如何断言 constexpr if else 子句永远不会发生?

如果条件为真,我想在非constexpr时引发编译时错误,例如:ifconstexpr(condition1){...}elseifconstexpr(condition2){....}elseifconstexpr(condition3){....}else{//Iwanttheelseclausenevertaken.ButIheardthecodebelowisnotallowedstatic_assert(false);}//I'drathernotrepeattheconditionsagainlikethis:static_assert(condition1||condit

c++ - 我应该将编译器生成的构造函数标记为 constexpr 吗?

做有区别吗:X()=default;和constexprX()=default;在常量表达式中默认构造类可以正常工作,那么这两个示例之间有区别吗?我应该使用其中一个吗? 最佳答案 由于在您的情况下,隐式构造函数实际上是constexpr...[C++11:12.1/6]:[..]Ifthatuser-writtendefaultconstructorwouldsatisfytherequirementsofaconstexprconstructor(7.1.5),theimplicitly-defineddefaultconstru

c++ - 我应该将编译器生成的构造函数标记为 constexpr 吗?

做有区别吗:X()=default;和constexprX()=default;在常量表达式中默认构造类可以正常工作,那么这两个示例之间有区别吗?我应该使用其中一个吗? 最佳答案 由于在您的情况下,隐式构造函数实际上是constexpr...[C++11:12.1/6]:[..]Ifthatuser-writtendefaultconstructorwouldsatisfytherequirementsofaconstexprconstructor(7.1.5),theimplicitly-defineddefaultconstru

c++ - 为什么非常量 std::array::operator[] 不是 constexpr?

我正在尝试使用给定函数在编译时填充二维数组。这是我的代码:templatestructTable{intdata[H][W];//std::array,W>data;//ThisdoesnotworkconstexprTable():data{}{for(inti=0;itable;//Ihavetable.dataproperlypopulatedatcompiletime它工作得很好,table.data在编译时正确填充。但是,如果我更改纯二维数组int[H][W]与std::array,W>,我在循环体中有错误:error:calltonon-constexprfunction'

c++ - 为什么非常量 std::array::operator[] 不是 constexpr?

我正在尝试使用给定函数在编译时填充二维数组。这是我的代码:templatestructTable{intdata[H][W];//std::array,W>data;//ThisdoesnotworkconstexprTable():data{}{for(inti=0;itable;//Ihavetable.dataproperlypopulatedatcompiletime它工作得很好,table.data在编译时正确填充。但是,如果我更改纯二维数组int[H][W]与std::array,W>,我在循环体中有错误:error:calltonon-constexprfunction'

c++ - 将默认赋值运算符声明为 constexpr : which compiler is right?

考虑structA1{constexprA1&operator=(constA1&)=default;~A1(){}};structA2{constexprA2&operator=(constA2&)=default;~A2()=default;};structA3{~A3()=default;constexprA3&operator=(constA3&)=default;};GCC和MSVC接受所有三个结构。Clang拒绝A1和A2(但接受A3),并带有以下错误消息::2:5:error:defaulteddefinitionofcopyassignmentoperatorisnot

c++ - 将默认赋值运算符声明为 constexpr : which compiler is right?

考虑structA1{constexprA1&operator=(constA1&)=default;~A1(){}};structA2{constexprA2&operator=(constA2&)=default;~A2()=default;};structA3{~A3()=default;constexprA3&operator=(constA3&)=default;};GCC和MSVC接受所有三个结构。Clang拒绝A1和A2(但接受A3),并带有以下错误消息::2:5:error:defaulteddefinitionofcopyassignmentoperatorisnot

c++ - 为什么 if constexpr 不会使这个核心常量表达式错误消失?

引用thisquestion.用于初始化constexpr变量y的核心常量表达式格式不正确。这么多是给定的。但是如果我尝试将if变成ifconstexpr:templatevoidfoo(){constexprintx=-1;ifconstexpr(x>=0){constexprinty=1();}错误仍然存​​在。GCC7.2仍然提供:error:rightoperandofshiftexpression'(1但我认为语义检查应该留在丢弃的分支上。通过constexprlambda进行间接处理确实有帮助,但是:templatevoidfoo(){constexprintx=-1;co