Title
Invalid example of adding special member function via default argument
Status
cd4
Section
6.3 [basic.def.odr]
Submitter
Hubert Tong

Created on 2015-02-13.00:00:00 last changed 95 months ago

Messages

Date: 2015-09-15.00:00:00

Proposed resolution (September, 2015):

Change 6.3 [basic.def.odr] bullet 6.6 as follows:

  • if D is a class with an implicitly-declared constructor (11.4.5 [class.ctor]), it is as if the constructor was implicitly defined in every translation unit where it is odr-used, and the implicit definition in every translation unit shall call the same constructor for a base class or a class member subobject of D. [Example:

  •   //translation unit 1:
      struct X {
        X(int, int);
        X(int, int, int);
      };
      X::X(int, int = 0) { }
      class D: public X {
        X x = 0;
      };
      D d2;            // X(int, int) called by D()
    
      //translation unit 2:
      struct X {
        X(int, int);
        X(int, int, int);
      };
      X::X(int, int = 0, int = 0) { }
      class D: public X {
        X x = 0;
      };
                       // X(int, int, int) called by D();
                       // D()'s implicit definition
                       // violates the ODR
    

    end example]

Date: 2016-02-15.00:00:00

[Adopted at the February, 2016 meeting.]

The example in 6.3 [basic.def.odr] bullet 6.6 reads,

  //translation unit 1:
  struct X {
    X(int);
    X(int, int);
  };
  X::X(int = 0) { }
  class D: public X { };
  D d2;     // X(int) called by D()

  //translation unit 2:
  struct X {
    X(int);
    X(int, int);
  };
  X::X(int = 0, int = 0) { }
  class D: public X { };   // X(int, int) called by D();
                           // D()'s implicit definition
                           // violates the ODR

Creating a special member function via default arguments added in an out-of-class definition, as is done here, is no longer permitted, so at a minimum the example should be removed. It is not clear whether there remain any cases to which the normative wording of bullet 6.6 would apply:

  • if D is a class with an implicitly-declared constructor (11.4.5 [class.ctor]), it is as if the constructor was implicitly defined in every translation unit where it is odr-used, and the implicit definition in every translation unit shall call the same constructor for a base class or a class member of D.

If not, the entire bullet should be removed.

History
Date User Action Args
2017-02-06 00:00:00adminsetstatus: tentatively ready -> cd4
2015-11-10 00:00:00adminsetmessages: + msg5588
2015-11-10 00:00:00adminsetstatus: drafting -> tentatively ready
2015-02-13 00:00:00admincreate