explicit specifier

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Specifies constructors and (since C++11) conversion operators that don't allow implicit conversions or copy-initialization.

[edit] Syntax

explicit class_name ( params )
explicit operator type ( ) (since C++11)
1) specifies that this constructor is only considered for direct initialization (including explicit conversions)
2) specifies that this user-defined conversion function is only considered for direct initialization (including explicit conversions)

[edit] Notes

A constructor with a single non-default parameter (until C++11) that is declared without the function specifier explicit is called a converting constructor.

An explicit default constructor can be used to perform both default initialization and value initialization: the use of explicit with a default constructor has no effect unless it has default arguments that would allow it to be used in copy-initialization context.

[edit] Example

struct A
{
    A(int) {} // converting constructor
    A(int, int) {} // converting constructor (C++11)
    operator int() const { return 0; }
};
 
struct B
{
    explicit B(int) {}
    explicit B(int, int) {}
    explicit operator int() const { return 0; }
};
 
int main()
{
    A a1 = 1; // OK: copy-initialization selects A::A(int)
    A a2(2); // OK: direct-initialization selects A::A(int)
    A a3 {4,5}; // OK: direct-list-initialization selects A::A(int, int)
    A a4 = {4,5}; // OK: copy-list-initialization selects A::A(int, int)
    int na1 = a1; // OK: copy-initialization selects A::operator int()
    int na2 = static_cast<int>(a1); // OK: static_cast performs direct-initialization
    A a5 = (A)1; // OK: explicit cast performs static_cast
 
//  B b1 = 1; // Error: copy-initialization does not consider B::B(int)
    B b2(2); // OK: direct-initialization selects B::B(int)
    B b3 {4,5}; // OK: direct-list-initialization selects A::A(int, int)
//  B b4 = {4,5}; // Error: copy-list-initialization does not consider B::B(int,int)
//  int nb1 = b2; // Error: copy-initialization does not consider B::operator int()
    int nb2 = static_cast<int>(b2); // OK: static_cast performs direct-initialization
    B b5 = (B)1; // OK: explicit cast performs static_cast
}