In this presentation: http://qtconference.kdab.com/sites/default/files/slides/mutz-dd-speed-up-your-qt-5-programs-using-c++11.pdf
The author suggests that N-ary constructors benefit from the C++11 version of explicit
keyword.
What changed in C++11 that makes this keyword useful if you have more than one constructor parameter?
In C++11, if you have a non-explicit constructor for a class A
that has multiple parameters (here I use A::A(std::string, int, std::string)
as an example), you can initialize an argument of that type with brace initialization:
void foo(A a);
foo({"the", 3, "parameters"});
Similarly, you can do the same with return values:
A bar() {
return {"the", 3, "parameters"};
}
If the constructor is, however, explicit
, these will not compile. Hence, the explicit
keyword now has importance for all constructors, rather than just conversion constructors.
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