A converting constructor is a constructor that enables implicit type conversion from its parameter type(s) to the class type. It allows the compiler to automatically initialize an object of the class when a value of the parameter type is provided in a context expecting the class type. When the compiler encounters an expression of typeDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
T where an object of class C is expected, it searches for a converting constructor in C that accepts T. If found, the compiler generates a user-defined conversion sequence. Under C++17’s guaranteed copy elision (prvalue materialization rules), implicit conversions during copy-initialization (e.g., passing by value or returning by value) directly initialize the target object in place; the compiler does not create or destroy any intermediate temporary objects.
Syntax and Mechanics
A constructor functions as a converting constructor if it is not declared with theexplicit specifier, or, starting in C++20, if it is declared with a conditionally explicit specifier that evaluates to false (explicit(false)). It is invoked during copy-initialization, pass-by-value function calls, and return-by-value statements.
C++11 and Multi-Argument Converting Constructors
Prior to C++11, only constructors callable with exactly one argument were considered converting constructors. Starting with C++11, constructors requiring multiple arguments also function as converting constructors when utilized with brace-enclosed initializer lists (copy-list-initialization).The explicit Specifier and C++20 explicit(bool)
To suppress implicit conversions, a constructor must be declared with the explicit keyword. An explicit constructor restricts object creation to direct-initialization, direct-list-initialization, or explicit type casting (e.g., static_cast).
C++20 introduced conditionally explicit constructors (explicit(bool)), which evaluate a boolean constant expression at compile time. This allows a constructor to act as a converting constructor only if specific type traits are satisfied.
Technical Constraints
- Single User-Defined Conversion: The C++ standard allows only one user-defined conversion sequence per implicit conversion. The compiler will not chain multiple converting constructors or combine a converting constructor with a user-defined conversion operator to resolve a type mismatch.
- Ambiguity and Standard Conversions: If multiple converting constructors can satisfy an implicit conversion via standard conversions of the same rank, the compiler will emit an ambiguous overload error. For example, passing an
intto a class that has both aConstructor(double)and aConstructor(float)results in ambiguity. Bothinttodoubleandinttofloatare standard floating-integral conversions of equal rank. The compiler cannot determine a best viable function, unlike with standard promotions (e.g.,shorttoint), which have a higher rank and are used to actively resolve overload ambiguities.
Master C++ with Deep Grasping Methodology!Learn More





