Search code examples
c++11nullptr

C++ nullptrt_t as argument in a constructor


Reading some code I found a class accepting just the new C++11 nullptr_t as parameter. The class looks like the one below.

Am I correct that the only thing I can construct an object by using exclusively nullptr?

class CA {
 public:
 CA(nullptr_t) {}  
};

Solution

  • Am I correct that the only thing I can construct an object by using exclusively nullptr?

    No. This is covered in §4.10 [conv.ptr]:

    A null pointer constant of integral type can be converted to a prvalue of type std::nullptr_t.

    where a null pointer constant is defined as follows:

    A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t.

    In other words, your constructor can be invoked also with various integer literals of value 0:

    CA{ 0 };
    CA{ 0u };
    CA{ 0LL };
    CA{ 0x0 };