Constructor templates vs default constructors
11.4.5 [class.ctor]
Richard Smith

Created on 2013-11-12.00:00:00 last changed 2 months ago


Date: 2024-04-15.00:00:00

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.

Date: 2016-02-15.00:00:00

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.

Date: 2014-02-15.00:00:00

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.

Date: 2013-11-12.00:00:00

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?

Date User Action Args
2024-04-11 21:11:41adminsetmessages: + msg7662
2017-02-06 00:00:00adminsetmessages: + msg5756
2017-02-06 00:00:00adminsetstatus: open -> drafting
2014-03-03 00:00:00adminsetmessages: + msg4913
2013-11-12 00:00:00admincreate