Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      explicit specifier

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      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 typebool


      1) Specifies that a constructoror conversion function(since C++11)ordeduction guide(since C++17) is explicit, that is, it cannot be used forimplicit conversions andcopy-initialization.
      2) Theexplicit specifier may be used with a constant expression. The function is explicit if and only if that constant expression evaluates totrue.
      (since C++20)

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

      [edit]Notes

      A constructorwith a single non-default parameter(until C++11) that is declared without the function specifierexplicit is called aconverting constructor.

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

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

      struct S{explicit(S)(const S&);// error in C++20, OK in C++17explicit(operatorint)();// error in C++20, OK in C++17};
      (since C++20)
      Feature-test macroValueStdFeature
      __cpp_conditional_explicit201806L(C++20)conditionalexplicit

      [edit]Keywords

      explicit

      [edit]Example

      Run this code
      struct A{    A(int){}// converting constructor    A(int,int){}// converting constructor (C++11)    operatorbool()const{returntrue;}}; struct B{explicit B(int){}explicit B(int,int){}explicit operatorbool()const{returntrue;}}; 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_castif(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_castif(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

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/explicit&oldid=174581"

      [8]ページ先頭

      ©2009-2025 Movatter.jp