C++ Reference ยท Types and Language Concepts
Implicit and explicit conversions
Reference for C++ conversions: standard implicit conversions, converting constructors, conversion operators, explicit, explicit(bool) and explicit casts.
A conversion changes a value from one type to another. C++ performs some conversions silently (implicit conversions) and others only when you ask for them (explicit conversions with a cast or a constructor call). For your own classes you control both: a constructor that can be called with one argument is a converting constructor, and a operator T() member is a conversion function.
Implicit conversions are convenient but can make overload resolution surprising and hide bugs. The explicit keyword turns a converting constructor or conversion operator into one that is used only when you write the conversion out.
struct Meters { double value; Meters(double v) : value(v) {} // implicit: double -> Meters explicit operator int() const { return static_cast<int>(value); } };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
Class(Arg); // converting constructor (implicit) explicit Class(Arg); // only direct initialization explicit(cond) Class(Arg); // C++20: explicit when cond is true operator Type() const; // conversion function (implicit) explicit operator Type() const; // C++11: explicit conversion function
| Kind | Description |
|---|---|
| Standard implicit | Integral and floating promotions, numeric conversions, array-to-pointer, function-to-pointer, derived-to-base pointers, pointer to bool, nullptr to pointers. |
| User-defined implicit | A converting constructor or non-explicit conversion operator. |
| Explicit | static_cast<T>(x), T(x), T{x}, C-style (T)x, and dynamic_cast, const_cast, reinterpret_cast for their special jobs. |
Contextual to bool |
if, while, for, !, &&, ` |
Usage notes
- At most one user-defined conversion is applied in an implicit conversion sequence, plus standard conversions before and after it.
"a.txt"toFilePathneedsconst char*tostd::string(user-defined) and then toFilePath(user-defined), which is two, so it fails. - Mark single-argument constructors
explicitunless the conversion is truly natural (std::stringfromconst char*is). Multi-argument constructors can beexplicittoo, to forbidreturn {1, 2};. - Prefer
explicit operator boolfor "is valid" tests. A non-explicitoperator boolletsobj1 + obj2andobj << 1compile through integer promotion. - Narrowing is rejected in brace initialization:
int i{3.5}is an error,int i = 3.5compiles (often with a warning). - Signed/unsigned mixing converts the signed operand to unsigned, so
-1 < 1uis false. Enable-Wsign-compare. - Conversion functions and overloads. Many viable conversion operators make calls ambiguous. Keep the set small.
- Conversion to the same class, a base class, or
voidthrough a conversion function never applies implicitly. explicit(bool)(C++20) lets templates choose:explicit(!std::is_convertible_v<U, T>)is howstd::pairandstd::optionalconstructors behave.- Unscoped enums convert implicitly to integers;
enum classvalues need a cast (orstd::to_underlying, C++23). - Pointers. Derived-to-base conversions are implicit, base-to-derived needs
static_castordynamic_cast.