0

I know that among other things, a trivial constructor has to be implicitly defined.

Does this also apply when we use the default keyword?

Say we specify a T()=default constructor , is it considered user-provided or is it treated like an implicit constructor?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Veritas
  • 2,150
  • 3
  • 16
  • 40

2 Answers2

4

Yes, a user-declared constructor that is defaulted on its first declaration may be trivial:

struct Foo
{ 
    Foo() = default;
    Foo(int, int);

    char x;
};

#include <type_traits>
static_assert(std::is_trivially_constructible<Foo>::value, "Works");

The example demonstrates how to define a POD class even in the presence of user-defined (non-default) constructors.

From the standard (12.1), "a default constructor is trivial if it is not user-provided" (plus conditions), and (8.4.2):

A function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration.

However, note that triviality of a default constructor depends on more than just its declaration and definition. To expand the quote from 12.1:

A default constructor is trivial if it is not user-provided and if:

— its class has no virtual functions (10.3) and no virtual base classes (10.1), and

— no non-static data member of its class has a brace-or-equal-initializer, and

— all the direct base classes of its class have trivial default constructors, and

— for all the non-static data members of its class that are of class type (or array thereof), each such class has a trivial default constructor.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • The standard quote about user provided constructors is exactly what I was looking for. I am aware of the other requirements but thanks for the efford. I guess I really should had worded the question better. – Veritas Apr 11 '14 at 08:51
0

Implicit constructor is one provided by the compiler if you don't define one. That's a default constructor having no argument, unless you would like to have your own constructor with or without arguments to precisely control initialization of your object instance data members.

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69