This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115d. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-10-26


1808. Constructor templates vs default constructors

Section: 11.4.5  [class.ctor]     Status: drafting     Submitter: Richard Smith     Date: 2013-11-12

It is not clear when, if ever, a constructor template can be considered to provide a default constructor. For example:

  struct A {
    template<typename ...T> A(T...); // #1
    A(std::initializer_list<long>);  // #2
  };
  A a{};

According to 9.4.5 [dcl.init.list] paragraph 3, A will be value-initialized if it has a default constructor, and there is implementation divergence whether this example calls #1 or #2.

Similarly, for an example like

  struct B {
    template<typename T=int> B(T = 0);
  };

it is not completely clear whether a default constructor should be implicitly declared or not.

More generally, do utterances in the Standard concerning “constructors” also apply to constructor templates?

Notes from the February, 2014 meeting:

One possibility discussed was that we may need to change places that explicitly refer to a default constructor to use overload resolution, similar to the change that was made a few years ago with regard to copy construction vs “copy constructor.” One additional use of “default constructor” is in determining the triviality of a class, but it might be a good idea to remove the concept of a trivial class altogether. This possibility will be explored.

Notes from the February, 2016 meeting:

CWG reaffirmed the direction from the preceding note and also determined that the presence of a constructor template should suppress implicit declaration of a default constructor.

Additional notes (April, 2024)

The standard does not seem to contain a use of "trivial class" outside of examples. See paper P3247R0 Deprecate the notion of trivial types.

Additional examples to consider for the definition of "trivial class":

  struct A {
    A() = default; //eligible, second constructor unsatisfied
    template<class... Args>
    A(Args&&... args) requires (sizeof...(Args) > 0) {}
  };

  struct B {
    B() = default; //ineligible, second constructor more constrained
    template<class... Args>
    B(Args&&... args) requires (sizeof...(Args) == 0) {}
  };

  struct C {
    C() = default; //eligible, but
    template<class... Args> //also eligible and non-trivial
    C(Args&&... args) {}
  };

See also issue 1363.