Skip to content
Tutorials Center
My learning

Topics

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.

C++
struct Meters {
    double value;
    Meters(double v) : value(v) {}           // implicit: double -> Meters
    explicit operator int() const { return static_cast<int>(value); }
};

Try it

C++ Standard conversions

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.

C++ Converting constructors and conversion operators

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

C++
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" to FilePath needs const char* to std::string (user-defined) and then to FilePath (user-defined), which is two, so it fails.
  • Mark single-argument constructors explicit unless the conversion is truly natural (std::string from const char* is). Multi-argument constructors can be explicit too, to forbid return {1, 2};.
  • Prefer explicit operator bool for "is valid" tests. A non-explicit operator bool lets obj1 + obj2 and obj << 1 compile through integer promotion.
  • Narrowing is rejected in brace initialization: int i{3.5} is an error, int i = 3.5 compiles (often with a warning).
  • Signed/unsigned mixing converts the signed operand to unsigned, so -1 < 1u is 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 void through a conversion function never applies implicitly.
  • explicit(bool) (C++20) lets templates choose: explicit(!std::is_convertible_v<U, T>) is how std::pair and std::optional constructors behave.
  • Unscoped enums convert implicitly to integers; enum class values need a cast (or std::to_underlying, C++23).
  • Pointers. Derived-to-base conversions are implicit, base-to-derived needs static_cast or dynamic_cast.