Implicitly default constructive meaning?

This man page for std::tuple says that the default constructor for a type is "explicit if and only if Ti not implicit by default constructive for at least one i ".

I'm a little confused about what this means "implicitly by default." Can someone give me an example?

+6
source share
2 answers

Here is 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 much more relevant since C ++ 11 thanks to list initialization.

+9
source

This means that for std::tuple<T1,T2,...,Tn> , all Ti types must be implicitly built by default.

Implicitly constructed default tuple example

For example, since std::string and std::vector implicitly constructed by default (their default constructor is not explicit ), std::tuple<std::string, std::vector> :

 void f(std::tuple<std::string, std::vector<int>>); f({}); // valid and equivalent to: std::string sempty; std::vector<int> vempty; auto tsv = std::make_tuple(sempty, vempty); f(tsv); 

Implicitly constructed default tuple example

With the implicit constructive default type A , std::tuple<std::string, A> cannot be implicitly built by default:

 struct A { explicit A() = default; }; void f(std::tuple<std::string, A>); f({}); // error 
+4
source

Source: https://habr.com/ru/post/1014290/


All Articles