-1

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*

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
gy gy
  • 13
  • 1
  • 2
    It calls the default constructor of `_Tp*`, which has almost no effect, except constructing a `nullptr`. – Ext3h Aug 27 '21 at 08:40
  • clearly, primitive fundamental types support the same default construction syntax as classes. But is there any advantage to the compiler (or other) over just writing `nullptr`? – Patrick Parker Aug 27 '21 at 08:45
  • Does this answer your question? [Do built-in types have default constructors?](https://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors) – Patrick Parker Aug 27 '21 at 08:58

2 Answers2

4

For all types, T() is an expression that value-initialises an unnamed instance of that type. For non-class, non-array types, value-initialisation is zero-initialisation

the object's initial value is the integral constant zero explicitly converted to T

And for a pointer type, that's a null pointer

Caleth
  • 52,200
  • 2
  • 44
  • 75
0

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335