C++11中nullptr的使用
在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.
在VS2013中,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 <iostream>
#include <cstddef> // for std::nullptr_t///
// reference: http://en.cppreference.com/w/cpp/language/nullptr
template<class F, class A>
void Fwd(F f, A a)
{f(a);
}void g(int* i)
{std::cout << "Function g called\n";
}int test_nullptr1()
{g(NULL); // Fineg(0); // FineFwd(g, nullptr); // Fine// Fwd(g, NULL); // ERROR: No function g(int) // error C2664: “void (int *)”: 無法將參數 1 從“int”轉換為“int *”int length1 = sizeof(NULL); // x64, length1 = 4int length2 = sizeof(nullptr); // x64, length2 = 8return 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 == nullptrif (pMyClass == 0)std::cout << "pMyClass == 0" << std::endl; // pMyClass == 0pMyClass = 0;if (pMyClass == nullptr)std::cout << "pMyClass == nullptr" << std::endl; // pMyClass == nullptrif (pMyClass == 0)std::cout << "pMyClass == 0" << std::endl; // pMyClass == 0return 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 insteadf((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 << '\n';elsestd::cout << "You passed in a null pointer\n";
}void doSomething_(std::nullptr_t ptr)
{std::cout << "in doSomething_()\n";
}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 pointerdoSomething(0); // You passed in a null pointerdoSomething(NULL); // You passed in a null pointer// call doSomething_ with an argument of type std::nullptr_tdoSomething_(nullptr); // in doSomething_()return 0;
}
GitHub:https://github.com/fengbingchun/Messy_Test
總結
以上是生活随笔為你收集整理的C++11中nullptr的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV代码提取:merge/spl
- 下一篇: C++11中auto的使用