Difference between revisions of "cpp/language/nullptr"
From cppreference.com
m (Text replace - "{{mark c++11 feature}}" to "{{mark since c++11}}") |
(Example added) |
||
Line 9: | Line 9: | ||
===Explanation=== | ===Explanation=== | ||
The keyword {{tt|nullptr}} denotes the null pointer literal. It is an unspecified prvalue of type {{cpp|std::nullptr_t}}. There exist {{rlp|implicit_cast|implicit conversions}} from {{tt|nullptr}} to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any value of type {{cpp|std::nullptr_t}} as well as for the macro {{cpp|NULL}}, the null pointer constant. | The keyword {{tt|nullptr}} denotes the null pointer literal. It is an unspecified prvalue of type {{cpp|std::nullptr_t}}. There exist {{rlp|implicit_cast|implicit conversions}} from {{tt|nullptr}} to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any value of type {{cpp|std::nullptr_t}} as well as for the macro {{cpp|NULL}}, the null pointer constant. | ||
+ | |||
+ | ===Example=== | ||
+ | {{example cpp | ||
+ | | Demonstrates how nullptr allows forwarding via a template function. | ||
+ | | code= | ||
+ | #include <cstddef> | ||
+ | #include <iostream> | ||
+ | |||
+ | template <typename F,typename A> | ||
+ | void Fwd( F f, A a ) | ||
+ | { | ||
+ | f( a ); | ||
+ | } | ||
+ | |||
+ | void g( int *i ) | ||
+ | { | ||
+ | std::cout << "Function g called\n"; | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | g( NULL ); // Fine | ||
+ | g( 0 ); // Fine | ||
+ | |||
+ | Fwd( g, nullptr ); // Fine | ||
+ | // Fwd( g, NULL ); // ERROR: No function g(int) | ||
+ | } | ||
+ | }} | ||
===Keywords=== | ===Keywords=== |
Revision as of 23:28, 1 March 2012
Contents |
Syntax
nullptr
|
(since C++11) | ||||||||
Explanation
The keyword nullptr
denotes the null pointer literal. It is an unspecified prvalue of type Template:cpp. There exist implicit conversions from nullptr
to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any value of type Template:cpp as well as for the macro Template:cpp, the null pointer constant.
Example
Keywords
See also
implementation-defined null pointer constant (macro constant) | |
(C++11) |
the type of the null pointer literal nullptr (typedef) |