The explicit keyword in C++ stops the compiler from using a constructor or conversion operator for implicit conversions and copy-initialisation. Mark single-argument constructors explicit so that a Meters object cannot silently be created from a plain double or int, which prevents surprising conversions and ambiguous overloads.
Use explicit by default for single-argument constructors, and drop it only when the implicit conversion is clearly intended, as with std::string from a string literal.
A constructor that can be called with one argument is a converting constructor. Without explicit, the compiler may call it automatically wherever a conversion is needed.
Passing a number where a buffer is expected is almost certainly a mistake, yet it compiles. Bugs like this appear when someone passes the wrong variable to a function.
Adding explicit disables implicit conversion, while direct construction still works.
The rule: an explicit constructor is considered only for direct-initialisation forms (T x(a), T x{a}, T(a), static_cast<T>(a)), not for T x = a or for passing an argument.
Before C++11, only single-argument constructors mattered. Since C++11, brace initialisation lets multi-argument constructors participate in implicit conversion from a braced list, so explicit is meaningful for them too.
A constructor with default arguments, such as Buffer(std::size_t size = 0), can also be called with one argument, so it should usually be explicit too. Consider explicit on a default constructor only when you want to forbid T x = {};.
Since C++11 you can mark conversion operators explicit. This is how std::unique_ptr and std::optional support use in conditions without converting to bool in arithmetic.
Contextual conversion allows an explicit operator bool in if, while, for conditions, the logical operators !, &&, ||, and the ternary condition. Before C++11 the "safe bool idiom" did this job awkwardly.
C++20 added explicit(condition), where the condition is a constant expression. Library authors use it to make a constructor explicit only for some types, the way std::pair and std::tuple do for converting constructors.
Here the constructor is implicit when U converts implicitly to T and explicit otherwise. You need <type_traits> for std::is_convertible_v.
Skip explicit when the conversion is natural and lossless, and callers expect it:
std::string from const char*.Celsius from Kelvin, when you design it deliberately.explicit; that would break pass-by-value and return-by-value.If in doubt, start with explicit. Removing it later is source compatible, while adding it later breaks callers.
Every single-argument constructor, by default. Copy and move constructors should not be. Multi-argument constructors should be explicit when you do not want return {a, b}; syntax to work.
No. Buffer(42) and static_cast<Buffer>(42) still work, because they are explicit conversions.
explicit only for constructors?No. It also applies to conversion operators, and in C++20 to deduction guides, where it controls copy-list-initialisation deduction.
No. It only affects which conversions the compiler accepts at compile time.
explicit prevents the compiler from using a constructor or conversion operator in implicit conversions. Apply it to single-argument constructors and to conversion operators such as operator bool, leave it off copy and move constructors, and use explicit(condition) in C++20 generic code. It turns accidental conversions into compile errors.