Key Takeaway
C++ explicit is a specifier that prevents constructors, conversion functions (since C++11), and deduction guides (since C++17) from participating in implicit conversions and copy-initialization. A constructor declared without explicit acts as a converting constructor, meaning a call like f(5) can silently convert a raw value into an object. The baseline guideline is to consider explicit first for single-argument constructors and omit it only when the conversion is conceptually natural, as in std::string s = "abc";. Contextual boolean conversions with explicit operator bool() (C++11) and conditional explicit(bool) (C++20) extend this exact principle.
This post examines what explicit blocks and what it permits, based on real compiler output from g++ 14.2 and clang 19.1.7. For the general rules governing implicit conversions, refer to the C++ integer promotion post (Korean).
What is C++ explicit? What is a converting constructor?
Interview one-liner: explicit prevents a constructor or conversion function from being used in implicit conversions and copy-initialization, and any constructor declared without it is a converting constructor.
- Official definition: cppreference specifies that
explicit“Specifies that a constructor or conversion function (since C++11) or deduction guide (since C++17) is explicit, that is, it cannot be used for implicit conversions and copy-initialization.” - Converting constructor: According to the cppreference page on converting constructors, a constructor declared without
explicitis a converting constructor. Prior to C++11, this referred to a constructor with a single non-default parameter. Since C++11, multi-argument non-explicit constructors also participate in copy-list-initialization. For example,A a4 = {4, 5};compiles, but if the constructor is markedexplicit,B b4 = {4, 5};produces a compile error. - Behavior with an explicit constructor: Copy-initialization such as
B b1 = 1;triggers a compile error. Direct-initialization such asB b2(2);andB b3{4, 5};, as well as explicit casts like(B)1, are valid. Similarly, with anexplicit operator bool, evaluating the object in a condition likeif (b2)works, whereas an implicit conversion likebool nb1 = b2;fails with a compile error; explicit casting withstatic_cast<bool>(b2)is required.

What bugs can happen without explicit?
Interview one-liner: Without explicit, values such as an int can silently convert into a temporary object and get passed to a function, often without producing any compiler warning.
- A real bug demo (
b.cpp): The following example uses a non-explicit constructor. Underg++ 14.2 -std=c++20 -Wall -Wextra, it compiled cleanly without warnings and executed:
#include <cstdio>
struct Buffer { Buffer(int size) : size(size) {} int size; };
void resize(Buffer b) { std::printf("Buffer size = %d\n", b.size); }
int main() { int count = 3; resize(count); }
The output is:
Buffer size = 3
No statement explicitly created a Buffer, yet the int variable count was silently converted into one.
-
Standard library design: This is precisely why
std::vector<int> v = 10;does not compile. The size-taking constructor ofstd::vectoris declaredexplicit, preventing a single integer from unintentionally initializing a 10-element container. -
Actual compile output: Here is
a.cpp, compiled usingg++ (Debian 14.2.0-19) 14.2.0andDebian clang 19.1.7with-std=c++20 -fsyntax-only:
#include <memory>
#include <vector>
struct Meters { explicit Meters(int v) : value(v) {} int value; };
void f(Meters m) {}
int main() {
Meters a{5}; // OK: direct-initialization
f(Meters{5}); // OK
Meters b = 5; // error
f(5); // error
std::vector<int> v = 10; // error
std::unique_ptr<int> p = std::make_unique<int>(1);
if (p) {} // OK: explicit operator bool in a condition
bool ok = p; // error
}
g++ 14.2 error output:
a.cpp:8:16: error: conversion from ‘int’ to non-scalar type ‘Meters’ requested
a.cpp:9:7: error: could not convert ‘5’ from ‘int’ to ‘Meters’
a.cpp:10:26: error: conversion from ‘int’ to non-scalar type ‘std::vector<int>’ requested
a.cpp:13:15: error: cannot convert ‘std::unique_ptr<int>’ to ‘bool’ in initialization
clang 19.1.7 output (first error and notes):
a.cpp:8:12: error: no viable conversion from 'int' to 'Meters'
a.cpp:3:26: note: explicit constructor is not a candidate
a.cpp:9:5: error: no matching function for call to 'f'
a.cpp:10:22: error: no viable conversion from 'int' to 'std::vector<int>'
- Inspecting compiler-generated conversions: To see how the compiler instantiates temporary objects under the hood, visit the C++ Insights post (Korean).
When should you use or omit explicit?
Interview one-liner: Consider explicit first for single-argument constructors, and omit it only when the conversion is conceptually natural, such as std::string s = "abc";.
- Default rule: For single-argument constructors, default to marking them
explicit. - When it is fine to omit: The
const char*constructor ofstd::stringis non-explicit, which allowsstd::string s = "abc";to work naturally. explicit operator bool(): It enables boolean checks in conditional contexts such asif (p)andwhile (p)while preventing unintended implicit conversions likebool ok = p;andint x = p;. Explicit conversions usingstatic_cast<bool>(p)remain available. Thestd::unique_ptrina.cppabove is an example.- C++20 conditional
explicit(bool):explicit(expression)makes the constructor explicit only when the constant bool expression evaluates to true; the feature-test macro is__cpp_conditional_explicit(201806L). Below isc.cpp, compiled with g++ 14.2-std=c++20:
template <class T> struct Wrap {
template <class U> explicit(!std::is_convertible_v<U, T>) Wrap(U u) : t(u) {}
T t;
};
struct S { explicit S(int) {} };
int main() { Wrap<long> a = 1; Wrap<S> b = 1; }
The only compile error was this single line:
c.cpp:7:44: error: conversion from ‘int’ to non-scalar type ‘Wrap<S>’ requested
Because int converts implicitly to long, the condition evaluates to explicit(false), and Wrap<long> a = 1; compiles without issue. In contrast, S has an explicit constructor, so int cannot implicitly convert to S; the condition evaluates to explicit(true), blocking only Wrap<S> b = 1;.

For the fundamentals of constructors themselves, see the copy and move constructor basics post (Korean).
How do you answer explicit questions in an interview?
Interview one-liner: Preparing the four questions below covers the vast majority of interview questions regarding explicit.
Q1. What is the role of the explicit keyword in C++, and why is it mainly used?
explicit stops a constructor, conversion function (since C++11), or deduction guide (since C++17) from participating in implicit conversions and copy-initialization. It prevents bugs from unintended conversions of raw values or other types into objects during function calls or assignments, enforcing direct-initialization (Meters m{5};) or explicit casts.
Q2. What is the difference between allowed and blocked initialization for a class with an explicit constructor?
Copy-initialization and implicit argument passing such as B b = 1;, B b = {4, 5};, and f(5) fail with compile errors. In contrast, direct-initialization such as B b(2); and B b{4, 5};, as well as explicit casts such as (B)1 and static_cast<B>(1), are permitted.
Q3. Why do smart pointers or wrapper classes declare explicit operator bool()?
It allows objects to be tested in conditional contexts like if (ptr) and while (ptr), while strictly blocking unsafe implicit conversions such as bool ok = ptr; or int x = ptr;.
Q4. When is it acceptable to omit explicit on a single-argument constructor?
When the conversion is conceptually natural and safe, such as std::string s = "abc";. The const char* constructor of std::string is declared non-explicit. The baseline rule still applies: always consider explicit first for single-argument constructors.