5

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
}
Ayrosa
  • 3,385
  • 1
  • 18
  • 29
  • I don't have an answer to your question, but note that `a = b + complex{1, 2};` works as a workaround – Cory Kramer Feb 11 '15 at 17:55
  • 1
    It would be nice to include the eror message. – jrok Feb 11 '15 at 17:59
  • look at http://en.cppreference.com/w/cpp/language/list_initialization which shows the contexts in which braced initialization can be used. – qeadz Feb 11 '15 at 17:59
  • The question needs to be asked the other way around. One can argue that nothing is supported unless the standard says it is supported. If you find something in the standard that says it should be supported but the compiler doesn't support it, then you have to ask why. – R Sahu Feb 11 '15 at 18:03
  • I assume that you already know this, but in case that you would like a workaround, here it is: `a = b + (complex){1, 2};` – Sergey Kalinichenko Feb 11 '15 at 18:04
  • Since I'm not feeling like using my dupehammer to reopen this - the answer is that the grammar for *assignment-expression* (*logical-or-expression assignment-operator initializer-clause*) allows for a *braced-init-list* on the RHS (*initializer-clause* is either an *assignment-expression* or a *braced-init-list*); the grammar for *additive-expression*s don't. Compare the grammar productions in [expr.add] and [expr.ass]; *initializer-clause* is defined in [dcl.init]. – T.C. Feb 11 '15 at 18:12
  • @dasblinkenlight That's a C99 compound literal, not C++. – T.C. Feb 11 '15 at 18:15
  • @T.C. [It does the trick in C++, though](http://ideone.com/CuOOOF) – Sergey Kalinichenko Feb 11 '15 at 18:16
  • @dasblinkenlight [Not with a sufficiently pedantic compiler](http://coliru.stacked-crooked.com/a/bc2baeb369be6231) :) – T.C. Feb 11 '15 at 18:17
  • @T.C. Your comment above was perfect. Thanks. – Ayrosa Feb 11 '15 at 18:25

1 Answers1

2

It is disallowed by not being listed in N3797 §8.5.4 [dcl.init.list]/1 (emphasis mine):

Note: List-initialization can be used

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

The emphasized bullet point corresponds to your a += {1, 3};. There is no point that fits an addition argument.

chris
  • 60,560
  • 13
  • 143
  • 205
  • 4
    Not being listed in a non-normative note is not normative. – T.C. Feb 11 '15 at 18:00
  • @T.C., Unfortunately, it's the best I have. I think the more correct answer would be that it is not allowed within any of those individual referenced sections. – chris Feb 11 '15 at 18:01
  • @chris The duplicate post answers the OP's question quite comprehensively IMHO – Cory Kramer Feb 11 '15 at 18:02
  • @Cyber, Oh snap, I didn't even see that. – chris Feb 11 '15 at 18:02
  • @Cyber I disagree. That question still doesn't cite anything normative. – T.C. Feb 11 '15 at 18:05
  • @T.C. No, but really the crux of the issue comes down to the grammar, and how to handle parsing commutative operations like this, which is what the answer explains. – Cory Kramer Feb 11 '15 at 18:06
  • @Cyber Yes, the answer to OP's question is "it's the grammar", and so the answer should be quoting the relevant parts of the grammar. – T.C. Feb 11 '15 at 18:10
  • @T.C. Would you mind to explain what you said above? – Ayrosa Feb 11 '15 at 18:11