NULL
From cppreference.net
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Type support
| Basic types | |||||||||||||||||||||
| Fixed width integer types (C++11) | |||||||||||||||||||||
| Fixed width floating-point types (C++23) | |||||||||||||||||||||
|
|||||||||||||||||||||
| Numeric limits | |||||||||||||||||||||
| C numeric limits interface | |||||||||||||||||||||
| Runtime type information | |||||||||||||||||||||
|
|||||||||||||||||||||
|
ヘッダーで定義
<clocale>
|
||
|
ヘッダーで定義
<cstddef>
|
||
|
ヘッダーで定義
<cstdio>
|
||
|
ヘッダーで定義
<cstdlib>
|
||
|
ヘッダーで定義
<cstring>
|
||
|
ヘッダーで定義
<ctime>
|
||
|
ヘッダーで定義
<cwchar>
|
||
|
#define NULL /* implementation-defined */
|
||
マクロ
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; // 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); // < 「未使用変数」警告を抑制します }
出力例:
The type of nullvar is long NULL implemented using an integral type
関連項目
| nullptr (C++11) | ヌルポインタ値を指定するポインタリテラル |
|
(C++11)
|
ヌルポインタリテラル
nullptr
の型
(typedef) |
|
Cドキュメント
for
NULL
|
|