Skip to content

Last updated:2026-10-13


comments: true keywords: C++ explicit constructor,explicit operator bool,explicit(bool),implicit conversion description: "Learn when C++ explicit blocks implicit conversions, how direct and copy initialization differ, and when C++20 explicit(bool) helps. Includes compilable examples." title: C++ explicit: Constructors, operator bool, and explicit(bool) | C++那些事


C++ explicit: constructors and conversion operators

Use explicit when an automatic conversion would hide a meaningful change of type. It applies to constructors and conversion functions, not ordinary methods. Direct initialization can use an explicit constructor; an implicit conversion cannot.

Direct versus copy initialization

#include <type_traits>

struct UserId {
    explicit UserId(int n) : value(n) {}
    int value;
};

void load(UserId) {}

int main() {
    UserId a(42);            // OK: direct initialization
    UserId b{42};            // OK: direct-list initialization
    load(UserId{42});        // OK: construction is visible
    // UserId c = 42;        // Error: copy initialization
    // load(42);             // Error: implicit int -> UserId conversion
    // UserId d = {42};      // Error: copy-list initialization
    static_assert(!std::is_convertible_v<int, UserId>);
    return a.value != b.value;
}

Compile with c++ -std=c++17 -Wall -Wextra explicit.cpp. Uncomment one rejected line at a time to see its diagnostic. An explicit constructor is still callable; callers must spell out the construction. This is useful for IDs, units and handles that should not be confused with plain integers.

explicit operator bool: safe conditions

struct Connection {
    explicit operator bool() const { return connected; }
    bool connected = true;
};

int main() {
    Connection c;
    if (c) {                 // OK: condition contextually converts to bool
        bool ready = static_cast<bool>(c); // OK: explicit conversion
        return ready ? 0 : 1;
    }
    // bool ready = c;       // Error: ordinary copy initialization
    return 1;
}

The condition in if (c) is a special contextual conversion to bool. It does not make c implicitly convertible to bool in every expression. Use this pattern when a type has a meaningful ready, valid or has-value state.

C++20: conditional explicit(bool)

Generic wrappers sometimes need to inherit a conversion policy. Since C++20, the constant expression in explicit(condition) determines whether a constructor is explicit.

#include <type_traits>

template<bool AllowImplicit>
struct Number {
    explicit(!AllowImplicit) Number(int n) : value(n) {}
    int value;
};

static_assert(std::is_convertible_v<int, Number<true>>);
static_assert(!std::is_convertible_v<int, Number<false>>);

int main() {
    Number<true> a = 7;     // OK: explicit(false)
    Number<false> b{7};     // OK: direct initialization
    // Number<false> c = 7;  // Error: explicit(true)
    return a.value != b.value;
}

Compile with c++ -std=c++20 -Wall -Wextra explicit20.cpp. If conditional behavior is unnecessary, plain explicit is easier to read.

Practical choice

Situation Choice
An ID or unit must not silently become another type Mark its converting constructor explicit
A wrapper has a usable or unusable state Consider explicit operator bool()
A template conditionally allows implicit conversion Use explicit(condition) in C++20

This guide expands the original repository example. For related type-design choices, see struct versus class and initialization versus assignment.

References

About the author

Personal WeChat official account:

The C++ explicit Keyword