Where in the C ++ standard is `a = b + {1, 2}` forbidden below in this fragment?

Where in the standard a = b + {1, 2} not indicated 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 } 
+5
source share
1 answer

This is prohibited without being listed in N3797 Β§8.5.4 [dcl.init.list] / 1 (emphasis mine):

Note: List initialization can be used.

  • as an initializer in the definition of a variable (8.5)
  • as an initializer in the new expression (5.3.4)
  • in the return statement (6.6.3)
  • as a range initializer (6.5)
  • as an argument to function (5.2.2)
  • as an index (5.2.1)
  • as an argument for calling the constructor (8.5, 5.2.3)
  • as an initializer for a non-static data element (9.2)
  • in mem-initializer (12.6.2)
  • on the right side of the assignment (5.17)

The underlined marker point matches your a += {1, 3}; . It makes no sense to enter an argument to add.

+2
source

Source: https://habr.com/ru/post/1213161/


All Articles