Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What C++0x Headers are supposed to define nullptr?

Now that C++0x is almost here, I've been experimenting with it, and in particular using nullptr. I haven't been able to figure out what standard header files one is supposed to include if one needs to use it.

Any help is appreciated.

like image 595
swestrup Avatar asked Apr 05 '11 05:04

swestrup


People also ask

How do you define a nullptr in C++?

The nullptr keyword represents a null pointer value. Use a null pointer value to indicate that an object handle, interior pointer, or native pointer type does not point to an object. Use nullptr with either managed or native code.

What address is nullptr?

Memory address 0 is called the null pointer. Your program is never allowed to look at or store anything into memory address 0, so the null pointer is a way of saying "a pointer to nothing".

Is nullptr == nullptr?

In C++11 and beyond, a pointer that is ==NULL will also ==nullptr and vice versa. Uses of NULL other than comparing with a pointer (like using it to represent the nul byte at the end of a string) won't work with nullptr .

What is the type of nullptr literals in C++?

The keyword nullptr denotes the null pointer literal. It is an unspecified prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type.


1 Answers

No headers should be required. It is a built-in keyword (§[lex.nullptr]).

2.14.7 Pointer literals                 [lex.nullptr]

pointer-literal:
        nullptr

The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See 4.10 and 4.11. —endnote]


Its type, std::nullptr_t, however, is "defined" in the header <cstddef> (§[support.types]/9).

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. [Note: Although nullptr’s address cannot be taken, the address of another nullptr_t object that is an lvalue can be taken. —endnote]

like image 127
kennytm Avatar answered Oct 21 '22 08:10

kennytm