名前空間
変種
操作

NULL

From cppreference.com
< cpp‎ | types
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 
ヘッダー <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) [編集]
English 日本語 中文(简体) 中文(繁體)