Converting constructor
| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
| Jump statements | ||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
| Dynamic exception specifications (until C++17*) | ||||||||||||||||
noexcept specifier (C++11) | ||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
| ||||||||||||||||
| Storage duration specifiers | ||||||||||||||||
| Initialization | ||||||||||||||||
| Expressions | ||||||||||||||||
| Alternative representations | ||||||||||||||||
| Literals | ||||||||||||||||
| Boolean - Integer - Floating-point | ||||||||||||||||
| Character - String - nullptr (C++11) | ||||||||||||||||
| User-defined (C++11) | ||||||||||||||||
| Utilities | ||||||||||||||||
| Attributes (C++11) | ||||||||||||||||
| Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
| Type alias declaration (C++11) | ||||||||||||||||
| Casts | ||||||||||||||||
| Memory allocation | ||||||||||||||||
| Classes | ||||||||||||||||
| Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
| Special member functions | ||||||||||||||||
| ||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
| General | ||||
| Overview | ||||
class/struct types | ||||
union types | ||||
| Injected-class-name | ||||
| Class property specifiers (C++26) | ||||
| Members | ||||
| Data members | ||||
| Static members | ||||
The this pointer | ||||
| Nested classes | ||||
| Member templates | ||||
| Bit-fields | ||||
using-declarations | ||||
| Member functions | ||||
| Member access specifiers | ||||
| Constructors and member initializer lists | ||||
| Default member initializer (C++11) | ||||
friend specifier | ||||
explicit specifier | ||||
| Converting constructor | ||||
| Special member functions | ||||
| Default constructor | ||||
| Copy constructor | ||||
| Move constructor (C++11) | ||||
| Copy assignment operator | ||||
| Move assignment operator (C++11) | ||||
| Destructor | ||||
| Inheritance | ||||
| Base and derived classes | ||||
| Empty base optimization (EBO) | ||||
| Virtual member functions | ||||
| Pure virtual functions and abstract classes | ||||
override specifier (C++11) | ||||
final specifier (C++11) |
A constructor that is not declared with the specifier explicit and which can be called with a single parameter(until C++11) is called a converting constructor.
Unlike explicit constructors, which are only considered during direct initialization (which includes explicit conversions such as static_cast), converting constructors are also considered during copy initialization, as part of user-defined conversion sequence.
It is said that a converting constructor specifies an implicit conversion from the types of its arguments (if any) to the type of its class. Note that non-explicit user-defined conversion function also specifies an implicit conversion.
Implicitly-declared and user-defined non-explicit copy constructors and move constructors are converting constructors.
[edit] Example
struct A { A() { } / converting constructor (since C++11) A(int) { } / converting constructor A(int, int) { } / converting constructor (since C++11) }; struct B { explicit B() { } explicit B(int) { } explicit B(int, int) { } }; int main() { A a1 = 1; / OK: copy-initialization selects A::A(int) A a2(2); / OK: direct-initialization selects A::A(int) A a3{4, 5}; / OK: direct-list-initialization selects A::A(int, int) A a4 = {4, 5}; / OK: copy-list-initialization selects A::A(int, int) A a5 = (A)1; / OK: explicit cast performs static_cast, direct-initialization / B b1 = 1; / error: copy-initialization does not consider B::B(int) B b2(2); / OK: direct-initialization selects B::B(int) B b3{4, 5}; / OK: direct-list-initialization selects B::B(int, int) / B b4 = {4, 5}; / error: copy-list-initialization selected an explicit constructor / B::B(int, int) B b5 = (B)1; / OK: explicit cast performs static_cast, direct-initialization B b6; / OK, default-initialization B b7{}; / OK, direct-list-initialization / B b8 = {}; / error: copy-list-initialization selected an explicit constructor / B::B() [](...){}(a1, a4, a4, a5, b5); / may suppress "unused variable" warnings }