NULL
From cppreference.com
| ヘッダー <clocale> で定義 |
||
| ヘッダ <cstddef> で定義 |
||
| ヘッダ <cstdio>で定義 |
||
| ヘッダ <cstdlib> で定義 |
||
| ヘッダー <cstring> で定義 |
||
| ヘッダ <ctime> で定義 |
||
| ヘッダ <cwchar> で定義 |
||
| #define NULL /* 実装定義 */ |
||
マクロNULLは、実装定義のヌルポインタ定数です。
目次 |
[編集] 可能な実装
#define NULL 0 // since C++11 #define NULL nullptr |
[編集] 備考
Cでは、マクロNULLは型void*を持つことができますが、C++ではヌルポインタ定数がその型を持つことができないため、これは許可されていません。
[編集] 例
このコードを実行
#include <cstddef> #include <iostream> #include <type_traits> #include <typeinfo> class S; int main() { int* p = NULL; int* p2 = static_cast<std::nullptr_t>(NULL); void(*f)(int) = NULL; int S::*mp = NULL; void(S::*mfp)(int) = NULL; auto nullvar = NULL; // may trigger a warning when compiling with gcc/clang std::cout << "The type of nullvar is " << typeid(nullvar).name() << '\n'; if constexpr(std::is_same_v<decltype(NULL), std::nullptr_t>) std::cout << "NULL implemented with type std::nullptr_t\n"; else std::cout << "NULL implemented using an integral type\n"; [](...){}(p, p2, f, mp, mfp); // < suppresses "unused variable" warnings }
実行結果の例
The type of nullvar is long NULL implemented using an integral type
[編集] 関連項目
| nullptr (C++11) | ヌルポインタ値を指定するポインタリテラル |
| (C++11) |
ヌルポインタ リテラル nullptr の型 (typedef) |
| NULLのCドキュメント
| |