Is nullptr_t a default constructible type?

前端 未结 2 1747
孤城傲影
孤城傲影 2021-02-13 01:53

I can\'t tell from the C++11 Standard if nullptr_t has a default constructor. In other words, is the following valid?:

nullptr_t n; 

GCC and VC

相关标签:
2条回答
  • 2021-02-13 02:33

    What the Standard says

    The standard says (18.2)

    nullptr_t is defined as follows:

    namespace std {
       typedef decltype(nullptr) nullptr_t;
    }
    

    The type for which nullptr_t is a synonym has the characteristics described in 3.9.1 and 4.10.

    Where 3.9.1 basically says it should be of the same size as void* and 4.10 specifies the conversion rules for nullptr.

    Edit: 3.9.9 furthermore explicitly states that nullptr_t is a scalar type, which means the expected initialization rules for built-in types from 8.5 apply:

    • Default-initialization (nullptr_t n;), which leaves the value of n undefined. As Johannes Schaub pointed out correctly, this compiles fine with the newest version of Clang.
    • Value-initialization (nullptr_t n = nullptr_t();), which initializes n to 0.

    This behavior is identical to e.g. int, so nullptr_t is definitely default-constructible. The interesting question here is: What does it mean for nullptr_t to have undefined value? At the end of the day, there is only one meaningful possible value for nullptr_t, which is nullptr. Furthermore the type itself is only defined through the semantics of the nullptrliteral. Do these semantics still apply for an unitialized value?

    Why that question doesn't matter in practice

    You don't want to declare a new variable of type nullptr_t. The only meaningful semantic of that type is already expressed through the nullptr literal, so whenever you would use your custom variable of type nullptr_t, you can just as well use nullptr.

    What does matter in practice

    The only exception to this comes from the fact that you can take non-type template parameters of type nullptr_t. For this case, it is useful to know which values can convert to nullptr_t, which is described in 4.10:

    A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. [...] A null pointer constant of integral type can be converted to a prvalue of type std::nullptr_t.

    Which basically does just what you'd expect: You can write

    nullptr_t n = 0;    // correct: 0 is special
    

    but not

    nullptr_t n = 42;   // WRONG can't convert int to nullptr_t
    

    Both gcc 4.6 and Clang SVN get this right.

    0 讨论(0)
  • 2021-02-13 02:38

    While nullptr is a new addition to the language itself, std::nullptr_t is just an alias of an unnamed type, the alias declared in cstddef like this:

    typedef decltype(nullptr) nullptr_t;
    

    While nullptr_t, being a typedef and not a language keyword, is not listed as a fundamental type, it is specified to behave as a fundamental type (and not, for example, as a pointer type or a class type). Therefore it does not have a default constructor, but you can still declare a variable like you have done. Your variable is not initialized and I wonder what its use could be and what error message you exactly got from clang.

    See also here.

    0 讨论(0)
提交回复
热议问题