What is the difference between nullptr and nullptr_t in C ++?

Which should i use? Any benefits if I use one over the other?

+4
source share
7 answers

nullptr- constant nullptr_t- its type. Use each of these in contexts where you need a null pointer, or a type of null pointer.

+14
source

nullptrhas type nullptr_t.

+4
source

"... ?"

( ), :

nullptr_t - , nullptr

nullptr (1) nullptr_t, .

++ 11:

2.14.7

  • - nullptr. std::nullptr_t.
    [: std::nullptr_t- , , ; , prvalue . . 4.10 4.11. - ]

1) , this nullptr r, const. , decltype(nullptr) const. Visual ++ 2015 MinGW g++ 5.1 const.

+3

, true ++ bool, nullptr ++ std::nullptr_t.

+3

nullptr - - std::nullptr_t. nullptr ++ , false true.:)

+2

[lex.nullptr]:

:
    nullptr

nullptr. std::nullptr_t. [: std::nullptr_t- , , ; , . . 4.10 4.11. -end note]

Therefore, use nullptrwhen you need a literal pointer, and std::nullptr_tin the context when you need to take this type. The latter, for example, if you create a function or constructor or something that can take nullptras an argument.

+2
source

If you try this one

cout << typeid(nullptr).name() << endl;

you will see that nullptr is of type std :: nullptr_t.

+1
source

All Articles