Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/explicit"

From cppreference.com
< cpp‎ | language
(c -> c/core.)
 
(33 intermediate revisions by 14 users not shown)
Line 1: Line 1:
{{title|explicit specifier}}
+
{{title|{{tt|explicit}} specifier}}
{{cpp/language/navbar}}
+
{{cpp/language/classes/navbar}}
 
+
Specifies constructors and {{mark since c++11}} conversion operators that don't allow [[cpp/language/implicit_cast|implicit conversions]] or [[cpp/language/copy_initialization|copy-initialization]].
+
  
 
===Syntax===
 
===Syntax===
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc | {{c|explicit}} {{spar|class_name}} ( {{spar|params}} ) }}
+
{{sdsc|num=1|
{{sdsc  | {{c|explicit operator}} {{spar|type}} ( ) {{mark since c++11}} }}
+
{{ttb|explicit}}
 +
}}
 +
{{sdsc|num=2|notes={{mark since c++20}}|
 +
{{ttb|explicit (}} {{spar|expression}} {{ttb|)}}
 +
}}
 
{{sdsc end}}
 
{{sdsc end}}
 +
{{par begin}}
 +
{{par|{{spar|expression}}|{{rlp|constant expression#Converted constant expression|contextually converted constant expression of type {{c/core|bool}}}}}}
 +
{{par end}}
  
@1@ specifies that this constructor is only considered for {{rlp|direct initialization}}
 
@2@ specifies that this {{rlp|cast_operator|user-defined conversion function}} is only considered for {{rlp|direct initialization}}
 
  
===Explanation===
+
@1@ Specifies that a constructor {{rev inl|since=c++11|or conversion function}}{{rev inl|since=c++17|or {{rlp|ctad|deduction guide}}}} is explicit, that is, it cannot be used for {{rlp|implicit conversion}}s and {{rlp|copy initialization|copy-initialization}}.
{{c|explicit}} on a constructor with multiple arguments has no effect, since such constructors cannot take part in implicit conversions. However, for the purpose of implicit conversion, explicit will have an effect if a constructor has multiple arguments and all but one of the arguments has a default value.
+
  
A constructor with a single-parameter constructor that is declared without the function specifier {{c|explicit}} is called a {{rlp|conversion constructor|conversion constructor}}.
+
{{rrev|since=c++20|
 +
@2@ The {{c/core|explicit}} specifier may be used with a constant expression. The function is explicit if and only if that constant expression evaluates to {{c|true}}.
 +
}}
 +
 
 +
The explicit specifier may only appear within the {{spar|decl-specifier-seq}} of the declaration of a constructor {{rev inl|since=c++11|or conversion function}} within its class definition.
 +
 
 +
===Notes===
 +
A constructor {{rev inl|until=c++11|with a single non-default parameter}} that is declared without the function specifier {{c/core|explicit}} is called a {{rlp|converting constructor}}.
 +
 
 +
Both constructors (other than {{rlp|copy constructor|copy}}/{{rlp|move constructor|move}}) and user-defined conversion functions may be function templates; the meaning of {{c/core|explicit}} does not change.
 +
 
 +
{{rrev|since=c++20|1=
 +
A {{ttb|(}} token that follows {{c/core|explicit}} is always parsed as part of the explicit specifier:
 +
{{source|1=
 +
struct S
 +
{
 +
    explicit (S)(const S&);    // error in C++20, OK in C++17
 +
    explicit (operator int)(); // error in C++20, OK in C++17
 +
};
 +
}}
 +
}}
 +
{{feature test macro|value=201806L|std=C++20|__cpp_conditional_explicit|conditional {{c/core|explicit}}}}
  
{{todo}}
+
===Keywords===
 +
{{ltt|cpp/keyword/explicit}}
  
 
===Example===
 
===Example===
Line 24: Line 48:
 
struct A
 
struct A
 
{
 
{
     A(int) {}
+
     A(int) {}     // converting constructor
     operator int() const { return 0; }
+
     A(int, int) {} // converting constructor (C++11)
 +
    operator bool() const { return true; }
 
};
 
};
  
Line 31: Line 56:
 
{
 
{
 
     explicit B(int) {}
 
     explicit B(int) {}
     explicit operator int() const { return 0; }
+
     explicit B(int, int) {}
 +
    explicit operator bool() const { return true; }
 
};
 
};
  
 
int main()
 
int main()
 
{
 
{
    // A has no explicit ctor / conversion, everything is fine
+
     A a1 = 1;     // OK: copy-initialization selects A::A(int)
     A a1 = 1; // OK: implicit conversion from int to B
+
     A a2(2);       // OK: direct-initialization selects A::A(int)
     A a2(2); // OK: explicit constructor call
+
     A a3 {4, 5};   // OK: direct-list-initialization selects A::A(int, int)
     A a3 { 3 }; // OK: explicit constructor call
+
     A a4 = {4, 5}; // OK: copy-list-initialization selects A::A(int, int)
     int na1 = a1; // OK: implicit conversion from B to int
+
    A a5 = (A)1;  // OK: explicit cast performs static_cast
     int na2 = static_cast<int>( a1 ); // OK: explicit cast
+
    if (a1) { }    // OK: A::operator bool()
+
    bool na1 = a1; // OK: copy-initialization selects A::operator bool()
     B b1 = 1; // Error: implicit conversion from int to B
+
     bool na2 = static_cast<bool>(a1); // OK: static_cast performs direct-initialization
     B b2(2); // OK: explicit constructor call
+
      
     B b3 { 3 }; // OK: explicit constructor call
+
//  B b1 = 1;     // error: copy-initialization does not consider B::B(int)
    int nb1 = b2; // Error: implicit conversion from B to int
+
     B b2(2);       // OK: direct-initialization selects B::B(int)
     int nb2 = static_cast<int>(b2); // OK: explicit cast
+
     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)
     B b4 = static_cast<B>(1); // OK: explicit conversion from int to B
+
     B b5 = (B)1;   // OK: explicit cast performs static_cast
 +
     if (b2) { }    // OK: B::operator bool()
 +
//  bool nb1 = b2; // error: copy-initialization does not consider B::operator bool()
 +
    bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization
 +
   
 +
    [](...){}(a4, a5, na1, na2, b5, nb2); // suppresses “unused variable” warnings
 
}
 
}
 
}}
 
}}
  
[[de:cpp/language/explicit]]
+
===See also===
[[es:cpp/language/explicit]]
+
* {{rlp|converting constructor}}
[[fr:cpp/language/explicit]]
+
* {{rlp|initialization}}
[[it:cpp/language/explicit]]
+
* {{rlp|copy initialization}}
[[ja:cpp/language/explicit]]
+
* {{rlp|direct initialization}}
[[pt:cpp/language/explicit]]
+
 
[[ru:cpp/language/explicit]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[zh:cpp/language/explicit]]
+

Latest revision as of 23:29, 11 August 2024

 
 
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
 
 

Contents

[edit] Syntax

explicit (1)
explicit ( expression ) (2) (since C++20)
expression - contextually converted constant expression of type bool


1) 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.
2) The explicit specifier may be used with a constant expression. The function is explicit if and only if that constant expression evaluates to true.
(since C++20)

The explicit specifier may only appear within the decl-specifier-seq of the declaration of a constructor or conversion function(since C++11) within its class definition.

[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.

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

A ( token that follows explicit is always parsed as part of the explicit specifier:

struct S
{
    explicit (S)(const S&);    // error in C++20, OK in C++17
    explicit (operator int)(); // error in C++20, OK in C++17
};
(since C++20)
Feature-test macro Value Std Feature
__cpp_conditional_explicit 201806L (C++20) conditional explicit

[edit] Keywords

explicit

[edit] Example

struct A
{
    A(int) {}      // converting constructor
    A(int, int) {} // converting constructor (C++11)
    operator bool() const { return true; }
};
 
struct B
{
    explicit B(int) {}
    explicit B(int, int) {}
    explicit operator bool() const { return true; }
};
 
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)
    A a5 = (A)1;   // OK: explicit cast performs static_cast
    if (a1) { }    // OK: A::operator bool()
    bool na1 = a1; // OK: copy-initialization selects A::operator bool()
    bool na2 = static_cast<bool>(a1); // OK: static_cast performs direct-initialization
 
//  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)
    B b5 = (B)1;   // OK: explicit cast performs static_cast
    if (b2) { }    // OK: B::operator bool()
//  bool nb1 = b2; // error: copy-initialization does not consider B::operator bool()
    bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization
 
    [](...){}(a4, a5, na1, na2, b5, nb2); // suppresses “unused variable” warnings
}

[edit] See also