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?
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.
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() .
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.
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.
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).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With