Title
Imprecise rule for reference member initializer
Status
drafting
Section
9.4.2 [dcl.init.aggr]
Submitter
Richard Smith

Created on 2015-05-19.00:00:00 last changed 108 months ago

Messages

Date: 2015-05-19.00:00:00

According to 11.9.3 [class.base.init] paragraph 11,

A temporary expression bound to a reference member from a brace-or-equal-initializer is ill-formed. [Example:

  struct A {
    A() = default;          // OK
    A(int v) : v(v) { }     // OK
    const int& v = 42;      // OK
  };
  A a1;                     // error: ill-formed binding of temporary to reference
  A a2(1);                  // OK, unfortunately

end example]

The rule is intended to apply only if an actual initialization results in such a binding, but it could be read as applying to the declaration of A::v itself. It would be clearer if the restriction were moved into bullet 9.1, e.g.,

  • if the entity is a non-static data member that has a brace-or-equal-initializer and either

    • the constructor's class is a union (11.5 [class.union]), and no other variant member of that union is designated by a mem-initializer-id or

    • the constructor's class is not a union, and, if the entity is a member of an anonymous union, no other member of that union is designated by a mem-initializer-id,

    the entity is initialized as specified in 9.4 [dcl.init], and the program is ill-formed if the entity is a reference member and this initialization binds it to a temporary;

History
Date User Action Args
2015-05-19 00:00:00admincreate