草庐IT

compound-assignment

全部标签

c++ - i +=++i 在 C++0x 中是未定义的行为吗?

我非常相信我找到的解释是i=++iisnotundefined就C++0x而言,但我无法判断i+=++i的行为是否定义明确。有接盘侠吗? 最佳答案 使i=++i定义良好的推理同样可以用来证明i+=++i也必须是良好的——已定义。i+=++i等价于i+=(i+=1)并且新的排序规则要求赋值发生在i+=1子表达式的值计算之前。这意味着表达式i+=++i的结果必须与i=2*i+1的结果相同。编辑:我必须修改我的答案,因为行为毕竟是未定义的。i+=++i的行为是未定义的,因为子表达式i(左侧参数)和++的值计算i彼此之间是无序的,其中一个包

c++ - i +=++i 在 C++0x 中是未定义的行为吗?

我非常相信我找到的解释是i=++iisnotundefined就C++0x而言,但我无法判断i+=++i的行为是否定义明确。有接盘侠吗? 最佳答案 使i=++i定义良好的推理同样可以用来证明i+=++i也必须是良好的——已定义。i+=++i等价于i+=(i+=1)并且新的排序规则要求赋值发生在i+=1子表达式的值计算之前。这意味着表达式i+=++i的结果必须与i=2*i+1的结果相同。编辑:我必须修改我的答案,因为行为毕竟是未定义的。i+=++i的行为是未定义的,因为子表达式i(左侧参数)和++的值计算i彼此之间是无序的,其中一个包

c++ - 位域 "In-class initialization"结果为 "error: lvalue required as left operand of assignment"

structbitfield{inti=0;//okintj:8=0;//error:lvaluerequiredasleftoperandofassignment};使用C++11“类内初始化”功能初始化位域的正确语法是什么? 最佳答案 这是作为C++标准的核心问题1341提出的,但在2015年10月被C++核心工作组拒绝为NAD(“不是缺陷”)-参见http://open-std.org/JTC1/SC22/WG21/docs/cwg_closed.html#1341 关于c++-位

c++ - 位域 "In-class initialization"结果为 "error: lvalue required as left operand of assignment"

structbitfield{inti=0;//okintj:8=0;//error:lvaluerequiredasleftoperandofassignment};使用C++11“类内初始化”功能初始化位域的正确语法是什么? 最佳答案 这是作为C++标准的核心问题1341提出的,但在2015年10月被C++核心工作组拒绝为NAD(“不是缺陷”)-参见http://open-std.org/JTC1/SC22/WG21/docs/cwg_closed.html#1341 关于c++-位

C++1y/C++14 : Assignment to object outside its lifetime is not allowed in a constant expression?

根据当前草案,以下C++14/C++1y程序是否格式错误?#includetemplatestructliteral_array{Tdata[n];};templateconstexprliteral_arrayoperator+(literal_arraya,literal_arrayb){literal_arrayx;for(size_ti=0;ia={1,2,3};constexprliteral_arrayb={4,5};constexprautoc=a+b;}Clangtrunk(在撰写本文时)给出:error:constexprvariable'c'mustbeinitia

C++1y/C++14 : Assignment to object outside its lifetime is not allowed in a constant expression?

根据当前草案,以下C++14/C++1y程序是否格式错误?#includetemplatestructliteral_array{Tdata[n];};templateconstexprliteral_arrayoperator+(literal_arraya,literal_arrayb){literal_arrayx;for(size_ti=0;ia={1,2,3};constexprliteral_arrayb={4,5};constexprautoc=a+b;}Clangtrunk(在撰写本文时)给出:error:constexprvariable'c'mustbeinitia

c++ - 即使 move-assignment 是 noexcept,std::move_if_noexcept 也会调用 copy-assignment;为什么?

我试图尽可能接近强异常保证,但是在玩弄std::move_if_noexcept时,我遇到了一些看似奇怪的行为。尽管下面的类中的移动赋值操作符被标记为noexcept,复制赋值操作符在被调用时被调用带有相关函数的返回值。structA{A(){/*...*/}A(Aconst&){/*...*/}A&operator=(Aconst&)noexcept{log("copy-assign");return*this;}A&operator=(A&&)noexcept{log("move-assign");return*this;}staticvoidlog(charconst*msg){

c++ - 即使 move-assignment 是 noexcept,std::move_if_noexcept 也会调用 copy-assignment;为什么?

我试图尽可能接近强异常保证,但是在玩弄std::move_if_noexcept时,我遇到了一些看似奇怪的行为。尽管下面的类中的移动赋值操作符被标记为noexcept,复制赋值操作符在被调用时被调用带有相关函数的返回值。structA{A(){/*...*/}A(Aconst&){/*...*/}A&operator=(Aconst&)noexcept{log("copy-assign");return*this;}A&operator=(A&&)noexcept{log("move-assign");return*this;}staticvoidlog(charconst*msg){

c++ - 重载运算符 [] 并没有得到 "lvalue required as left operand of assignment"错误

这与所有“需要左值作为赋值的左操作数”错误问题有点相反。我有一个重载运算符[]的类,但只有返回临时的版本。如果要返回一个int:structFoo{intoperator[](intidx)const{returnint(0);}};Foof;f[1]=5;我会理所当然地得到左值编译器错误。但是,如果它返回一个结构类型,编译器(在这种情况下是GCC7.2)根本不会提示:structBar{};structFoo{Baroperator[](intidx)const{returnBar();}};Foof;f[1]=Bar();如果Bar是临时的并且没有专门的运算符=,为什么不会以同样的

c++ - 重载运算符 [] 并没有得到 "lvalue required as left operand of assignment"错误

这与所有“需要左值作为赋值的左操作数”错误问题有点相反。我有一个重载运算符[]的类,但只有返回临时的版本。如果要返回一个int:structFoo{intoperator[](intidx)const{returnint(0);}};Foof;f[1]=5;我会理所当然地得到左值编译器错误。但是,如果它返回一个结构类型,编译器(在这种情况下是GCC7.2)根本不会提示:structBar{};structFoo{Baroperator[](intidx)const{returnBar();}};Foof;f[1]=Bar();如果Bar是临时的并且没有专门的运算符=,为什么不会以同样的