Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

explicit — controlling implicit conversions

The explicit specifier restricts constructors and conversion functions in implicit conversions while preserving deliberate object construction. Learn the distinction between direct- and copy-initialization, the special role of operator bool, and conditional explicit(condition).

Version: C++23; repository compiler flag, production runtime not attested

Syntax

explicit ClassName(parameters);
explicit operator TargetType() const;
explicit(condition) ClassName(parameters);
explicit(condition) operator TargetType() const;

explicit helps restrict unintended user-defined conversions. It can mark a constructor or conversion function inside a class definition; it is not a specifier for ordinary functions. It can also appear on class template deduction guides. Plain explicit means explicit(true).

For a constructor explicit Count(int), both Count a(3); and Count b{3}; are valid direct-initialization. Count c = 3; is invalid because copy-initialization does not use that constructor for conversion. Count d = {3}; is also invalid if copy-list-initialization selects an explicit constructor. Similarly, a function taking Count cannot accept plain 3, but can receive Count{3}. The specifier is not limited to single-parameter constructors.

A conversion function explicit operator bool() const disallows bool e = a;, but allows static_cast<bool>(a) and direct-initialization bool e(a);. Boolean conditions are an important exception: if (a) can use this operator because contextual conversion to bool tests whether direct-initialization is possible. This does not generally enable implicit conversions.

In C++23, explicit(condition) has stricter rules than an ordinary if condition: the result of contextual conversion to bool must be a constant expression, and the conversion sequence may contain only permitted conversions, with no narrowing conversions. For example, 2 is a constant expression and works as the condition in if (2), but explicit(2) is ill-formed because converting 2 to bool is narrowing. A value of true enables explicit; false leaves the declaration non-explicit. This is not a runtime switch. It is useful in templates when implicit convertibility depends on a type. The example uses C++23 mode. The sources present an evolving working draft, not a frozen C++23 publication.

Examples

#include <iostream>
#include <type_traits>

struct Count {
    int value;
    explicit Count(int n) : value(n) {}
    explicit operator bool() const { return value != 0; }
};

static_assert(std::is_constructible_v<Count, int>);
static_assert(!std::is_convertible_v<int, Count>);
static_assert(!std::is_convertible_v<Count, bool>);

int main() {
    Count a(3);
    Count b{0};
    // Count c = 3;
    // Count d = {3};
    // bool e = a;
    if (a) {
        std::cout << a.value << '\n';
    }
    std::cout << std::boolalpha << static_cast<bool>(b) << '\n';
}

Result

3
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources