[class.conv.ctor] (original) (raw)
11 Classes [class]
11.4 Class members [class.mem]
11.4.8 Conversions [class.conv]
11.4.8.2 Conversion by constructor [class.conv.ctor]
A constructor that is not explicit ([dcl.fct.spec]) specifies a conversion from the types of its parameters (if any) to the type of its class.
[Example 1: struct X { X(int); X(const char*, int = 0); X(int, int);};void f(X arg) { X a = 1; X b = "Jessie"; a = 2; f(3); f({1, 2}); } — _end example_]
[Note 1:
An explicit constructor constructs objects just like non-explicit constructors, but does so only where the direct-initialization syntax ([dcl.init]) or where casts ([expr.static.cast], [expr.cast]) are explicitly used; see also [over.match.copy].
A default constructor can be an explicit constructor; such a constructor will be used to perform default-initialization or value-initialization ([dcl.init]).
[Example 2: struct Z { explicit Z();explicit Z(int);explicit Z(int, int);}; Z a; Z b{}; Z c = {}; Z a1 = 1; Z a3 = Z(1); Z a2(1); Z* p = new Z(1); Z a4 = (Z)1; Z a5 = static_cast<Z>(1); Z a6 = { 3, 4 }; — _end example_]
— _end note_]