Namespaces
Variants
Views
Actions

explicit specifier

From cppreference.com
< cpp‎ | language
Revision as of 06:25, 4 May 2015 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

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

Syntax

explicit class_name ( params ) (1)
explicit operator type ( ) (since C++11) (2)
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)

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.

Both constructors (other than copy/move) and user-defined conversion functions may be function templates; the meaning of explicit doesn't change.

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 B::B(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
}