本文介绍了在下面的代码段中不允许在C ++标准中使用`a = b + {1,2}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 标准中 a = b + {1,2} 以下不允许? class complex { double re,im; public: complex(double r,double i):re {r},im {i} {} complex& operator + =(const complex& other){re + = other.re; im + = other.im; return * this; } }; inline复数运算符+(复数lhs,const复数和rhs) { lhs + = rhs; return lhs; } int main() { complex a {1,1} complex b {2,-3}; a + = {1,3}; // Ok a = b + {1,2}; //不编译} 解决方案它不被列在N3797§8.5.4[dcl.init.list] / 1(强调我的): 强调的项目符号对应于 a + = {1,3; / code>。没有符合加法参数的点。 Where in the Standard is a = b + {1, 2} disallowed below?class complex { double re, im;public: complex(double r, double i) : re{ r }, im{ i } {} complex& operator+=(const complex& other) { re += other.re; im += other.im; return *this; }};inline complex operator+(complex lhs, const complex& rhs){ lhs += rhs; return lhs;}int main(){ complex a{ 1, 1 }; complex b{ 2, -3 }; a += {1, 3}; // Ok a = b + {1, 2}; // doesn't compile} 解决方案 It is disallowed by not being listed in N3797 §8.5.4 [dcl.init.list]/1 (emphasis mine):The emphasized bullet point corresponds to your a += {1, 3};. There is no point that fits an addition argument. 这篇关于在下面的代码段中不允许在C ++标准中使用`a = b + {1,2}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 07:30