Search code examples
c++pointersinitializationunique-ptrdefault-constructor

What is pointer() in unique_ptr?


I read now unique_ptr source code in libstdc++.

 public:
   typedef _Tp*               pointer;
   typedef _Tp                element_type;      
   typedef _Tp_Deleter        deleter_type;

   // Constructors.
   unique_ptr()
   : _M_t(pointer(), deleter_type())
   { static_assert(!std::is_pointer<deleter_type>::value,
           "constructed with null function pointer deleter"); }

I don´t understand.Does "pointer()" call constructor? but "pointer" is an alias for type _Tp*


Solution

  • This expression

    pointer()
    

    zero-initializes the data member pointer of the class that has a pointer type. For pointer types it means setting a pointer to a null pointer.

    From the C++ 14 Standard (8.5 Initializers)

    11 An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized

    and

    8 To value-initialize an object of type T means:

    (8.4) — otherwise, the object is zero-initialized.

    Further

    6 To zero-initialize an object or reference of type T means:

    (6.1) — if T is a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T;104

    and in the footnote 104 there is written

    1. As specified in 4.10, converting an integer literal whose value is 0 to a pointer type results in a null pointer value