C++11におけるnullptrの使用

5012 ワード

C言語では、NULLは実際にvoid*のポインタであり、void*のポインタを他のタイプのポインタに割り当てると、暗黙的に対応するタイプに変換されます.一方、C++コンパイラでコンパイルする場合はエラーが発生します.C++は強いタイプなので、void*は他のポインタタイプに暗黙的に変換できません.C++で空ポインタを決定する問題を理解するために、C++に0を導入して空ポインタを表す.NULLにはタイプがありません.マクロです.nullptrにはタイプがあり、タイプはstd::nullptr_t.
推薦:純粋なC言語はNULLを使います;C++用0;コンパイラがnullptrをサポートしている場合はnullptrを使用します.
The keyword nullptr denotes the pointer literal. It is a prvalue(pure rvalue) 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. Similar conversions exist for any null pointer constant, which includes values of type std::nullptr_t as well as the macro NULL.
Both true and false are keywords and as literals they have a type ( bool ). nullptr is a pointer literal of type std::nullptr_t, and it's a prvalue (you cannot take the address of it using &).
A null pointer constant with the nullptr value has the following characteristics:
(1)、It can be converted to any pointer or pointer-to-member type.
(2)、It cannot be implicitly converted to any other type, except for the bool type.
(3)、It cannot be used in an arithmetic expression.
(4)、It can be compared with the integer 0.
(5)、It can be used in relational expressions to compare with pointers or data of the std::nullptr_t type.
It should be noted that in C++11 it is still acceptable to assign the value 0 or NULL to a pointer.
VS 2013において、NULLの定義はstdioである.hファイルでは、以下のようになります.
/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else  /* __cplusplus */
#define NULL    ((void *)0)
#endif  /* __cplusplus */
#endif  /* NULL */

次は、他の記事のcopyのテストコードです.詳細は、対応するreferenceを参照してください.
#include "nullptr.hpp"

#include 
#include  // for std::nullptr_t

///////////////////////////////////////////
// reference: http://en.cppreference.com/w/cpp/language/nullptr
template
void Fwd(F f, A a)
{
	f(a);
}

void g(int* i)
{
	std::cout << "Function g called
"; } int test_nullptr1() { g(NULL); // Fine g(0); // Fine Fwd(g, nullptr); // Fine // Fwd(g, NULL); // ERROR: No function g(int) // error C2664: “void (int *)”: 1 “int” “int *” int length1 = sizeof(NULL); // x64, length1 = 4 int length2 = sizeof(nullptr); // x64, length2 = 8 return 0; } /////////////////////////////////////////// // reference: https://msdn.microsoft.com/zh-cn/library/4ex65770.aspx class MyClass { public: int i; }; int test_nullptr2() { MyClass * pMyClass = nullptr; if (pMyClass == nullptr) std::cout << "pMyClass == nullptr" << std::endl; // pMyClass == nullptr if (pMyClass == 0) std::cout << "pMyClass == 0" << std::endl; // pMyClass == 0 pMyClass = 0; if (pMyClass == nullptr) std::cout << "pMyClass == nullptr" << std::endl; // pMyClass == nullptr if (pMyClass == 0) std::cout << "pMyClass == 0" << std::endl; // pMyClass == 0 return 0; } ///////////////////////////////////////////////////// void f(int *) { std::cout << "f(int *)" << std::endl;; } void f(int &) { std::cout << "f(int &)" << std::endl; } int test_nullptr3() { f(nullptr); // f(int *) // try one of the following lines instead f((int *) nullptr); // f(int *) f(0); // f(int *) f(NULL); // f(int *) //f((int &) nullptr); // error C2101: “&” return 0; } ////////////////////////////////////////////// // reference: http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html void func(int n) { std::cout << "func (int n)" << std::endl; } void func(char *s) { std::cout << "func (char *s)" << std::endl; } int test_nullptr4() { func(0); // func (int n) func(NULL); // func (int n) func(nullptr); // func (char *s) return 0; } ////////////////////////////////////////////////////////// // reference: http://www.learncpp.com/cpp-tutorial/6-7a-null-pointers/ void doSomething(int *ptr) { if (ptr) std::cout << "You passed in " << *ptr << '
'; else std::cout << "You passed in a null pointer
"; } void doSomething_(std::nullptr_t ptr) { std::cout << "in doSomething_()
"; } int test_nullptr5() { int* a = NULL; // ok //int* b = (void*)0; // error C2440: “ ”: “void *” “int *” int* c = 0; // ok // the argument is definitely a null pointer (not an integer) doSomething(nullptr); // You passed in a null pointer doSomething(0); // You passed in a null pointer doSomething(NULL); // You passed in a null pointer // call doSomething_ with an argument of type std::nullptr_t doSomething_(nullptr); // in doSomething_() return 0; }

GitHub:https://github.com/fengbingchun/Messy_Test