Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicitly default-constructible meaning?

Tags:

c++

c++11

On this reference page for std::tuple it is said that the default constructor for the type is "explicit if and only if Ti is not implicitly default-constructible for at least one i".

I'm a bit confused what it means by "implicitly default-constructible". Can someone give me an example?

like image 971
Zizheng Tai Avatar asked Jan 21 '17 21:01

Zizheng Tai


People also ask

What is default constructible C++?

A default constructible class is a class that has a default constructor (either its implicit constructor or a custom defined one). The is_default_constructible class inherits from integral_constant as being either true_type or false_type, depending on whether T is default constructible.

What is implicit default constructor?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Is trivially default constructible?

A trivially default constructible type is a type which can be trivially constructed without arguments or initialization values, either cv-qualified or not. This includes scalar types, trivially default constructible classes and arrays of such types.

What is the purpose of a default constructor?

The default constructor in Java initializes the data members of the class to their default values such as 0 for int, 0.0 for double etc. This constructor is implemented by default by the Java compiler if there is no explicit constructor implemented by the user for the class.

What is a default constructible type in Java?

A default constructible type is a type which can be constructed without arguments or initialization values, either cv-qualified or not. This includes scalar types, default constructible classes and arrays of such types. A default constructible class is a class that has a default constructor (either its implicit constructor or a custom defined one).

How do we declare a default constructor implicitly in C++?

If you are looking for how do we declare a default constructor implicitly, here are details. In C++ programming, If there is no constructor in the class (in the struct or in the union), the C++ compiler will always declare a default constructor as an inline public member of that class.

What happens to the default constructor if it is not defined?

If the implicitly-declared default constructor is not defined as deleted, it is defined (that is, a function body is generated and compiled) by the compiler if odr-used, and it has exactly the same effect as a user-defined constructor with empty body and empty initializer list.

What is trivial default constructor in C?

A trivial default constructor is a constructor that performs no action. All data types compatible with the C language (POD types) are trivially default-constructible. A default constructor is eligible if it is either user-declared or both implicitly-declared and definable.


1 Answers

Here's an example:

struct A {};
struct B { explicit B() = default; };

int main()
{
    A a1 = {};
    A a2 {};

    // B b1 = {}; // Error, would use explicit default constructor
    B b2 {};
}

Constructors with explicit have become a lot more relevant since C++11 thanks to list-initialization.

like image 112
Kerrek SB Avatar answered Sep 18 '22 05:09

Kerrek SB