Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Value-initialization

      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
      Special member functions
      Templates
      Miscellaneous
       
       

      This is the initialization performed when an object is constructed with an empty initializer.

      Contents

      [edit]Syntax

      T() (1)
      newT() (2)
      Class::Class(...):member(){...} (3)
      Tobject{}; (4)(since C++11)
      T{} (5)(since C++11)
      newT{} (6)(since C++11)
      Class::Class(...):member{}{...} (7)(since C++11)

      [edit]Explanation

      Value-initialization is performed in these situations:

      1,5) when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses or braces(since C++11);
      2,6) when an object with dynamic storage duration is created by anew expression with the initializer consisting of an empty pair of parentheses or braces(since C++11);
      3,7) when a non-static data member or a base class is initialized using amember initializer with an empty pair of parenthesesor braces(since C++11);
      4) when a named object (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces.

      In all cases, if the empty pair of braces{} is used andT is an aggregate type,aggregate initialization is performed instead of value-initialization.

      IfT is a class type that has no default constructor but has a constructor takingstd::initializer_list,list-initialization is performed.

      (since C++11)

      The effects of value-initialization are:

      • IfT is a (possibly cv-qualified) class type:
      • Otherwise, ifT is an array type, each element of the array is value-initialized.
      • Otherwise, the object is zero-initialized.

      [edit]Notes

      The syntaxT object(); does not initialize an object; it declares a function that takes no arguments and returnsT. The way to value-initialize a named variable before C++11 wasT object= T();, which value-initializes a temporary and then copy-initializes the object: most compilersoptimize out the copy in this case.

      References cannot be value-initialized.

      As described infunction-style cast, the syntaxT()(1) is prohibited ifT names an array type, whileT{}(5) is allowed.

      All standard containers (std::vector,std::list, etc.) value-initialize their elements when constructed with a singlesize_type argument or when grown by a call toresize(), unless their allocator customizes the behavior ofconstruct.

      [edit]Example

      Run this code
      #include <cassert>#include <iostream>#include <string>#include <vector> struct T1{int mem1;std::string mem2;virtualvoid foo(){}// make sure T1 is not an aggregate};// implicit default constructor struct T2{int mem1;std::string mem2;    T2(const T2&){}// user-provided copy constructor};// no default constructor struct T3{int mem1;std::string mem2;    T3(){}// user-provided default constructor}; std::string s{};// class => default-initialization, the value is "" int main(){int n{};// scalar => zero-initialization, the value is 0assert(n==0);double f=double();// scalar => zero-initialization, the value is 0.0assert(f==0.0);int* a= newint[10]();// array => value-initialization of each elementassert(a[9]==0);//          the value of each element is 0    T1 t1{};// class with implicit default constructor =>assert(t1.mem1==0);//     t1.mem1 is zero-initialized, the value is 0assert(t1.mem2=="");//     t1.mem2 is default-initialized, the value is ""//  T2 t2{};                // error: class with no default constructor    T3 t3{};// class with user-provided default constructor =>std::cout<< t3.mem1;//     t3.mem1 is default-initialized to indeterminate valueassert(t3.mem2=="");//     t3.mem2 is default-initialized, the value is ""std::vector<int> v(3);// value-initialization of each elementassert(v[2]==0);// the value of each element is 0std::cout<<'\n';    delete[] a;}

      Possible output:

      42

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 178C++98there was no value-initialization; empty initializer invoked default-
      initialization (thoughnew T() also performs zero-initialization)
      empty initializer invoke
      value-initialization
      CWG 543C++98value-initialization for a class object without any
      user-provided constructors was equivalent to value-
      initializing each subobject (which need not zero-
      initialize a member with user-provided default constructor)
      zero-initializes
      the entire object,
      then calls the
      default constructor
      CWG 1301C++11value-initialization of unions with deleted
      default constructors led to zero-initialization
      they are
      default-initialized
      CWG 1368C++98any user-provided constructor caused
      zero-initialization to be skipped
      only a user-provided
      default constructor
      skips zero-initialization
      CWG 1502C++11value-initializing a union without a user-provided
      default constructor only zero-initialized the
      object, despite default member initializers
      performs default-
      initialization after
      zero-initialization
      CWG 1507C++98value-initialization for a class object without any
      user-provided constructors did not check the validity
      of the default constructor when the latter is trivial
      the validity of trivial
      default constructor
      is checked
      CWG 2820C++98the default-initialization following the zero-
      initialization required a non-trivial constructor
      not required
      CWG 2859C++98value-initialization for a class object might involve
      zero-initialization even if the default-initialization
      does not actually select a user-provided constructor
      there is no
      zero-initialization
      in this case

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp