草庐IT

mass-assignment

全部标签

c++ - 使用 boost::assign::list_of

这样编译:std::vectorvalue=boost::assign::list_of(1)(2);但不是这个:Constructor(std::vectorvalue){}Constructor(boost::assign::list_of(1)(2));是否有一种用于初始化传递给构造函数的vector的单线解决方案?更好的是,如果构造函数通过引用来复制到类变量:Constructor(std::vector&value){_value=value;}更新如果我尝试以下操作:enumFoo{FOO_ONE,FOO_TWO};classConstructor{public:Const

c++ - 使用 boost::assign::list_of

这样编译:std::vectorvalue=boost::assign::list_of(1)(2);但不是这个:Constructor(std::vectorvalue){}Constructor(boost::assign::list_of(1)(2));是否有一种用于初始化传递给构造函数的vector的单线解决方案?更好的是,如果构造函数通过引用来复制到类变量:Constructor(std::vector&value){_value=value;}更新如果我尝试以下操作:enumFoo{FOO_ONE,FOO_TWO};classConstructor{public:Const

c++ - STL vector : resize() and assign()

有一个类(class)成员std::vectorv和intn,在此vector上使用以下内容有什么区别?,未初始化:v.assign(n,0.0);或v.resize(n,0.0); 最佳答案 assign将大小设置为n并将所有元素值设置为0.0,而resize将大小设置为n并且只有new元素值为0.0。如果v事先为空,它们是相同的,但assign可能更清楚。 关于c++-STLvector:resize()andassign(),我们在StackOverflow上找到一个类似的问题:

c++ - STL vector : resize() and assign()

有一个类(class)成员std::vectorv和intn,在此vector上使用以下内容有什么区别?,未初始化:v.assign(n,0.0);或v.resize(n,0.0); 最佳答案 assign将大小设置为n并将所有元素值设置为0.0,而resize将大小设置为n并且只有new元素值为0.0。如果v事先为空,它们是相同的,但assign可能更清楚。 关于c++-STLvector:resize()andassign(),我们在StackOverflow上找到一个类似的问题:

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){