nullptr、ポインタ リテラル (C++11 以降)
From cppreference.com
目次 |
[編集] 構文
nullptr
|
|||||||||
[編集] 説明
キーワード nullptr はポインタ リテラルを示します。std::nullptr_t 型のprvalueです。nullptr から、任意のポインタ型および任意のメンバへのポインタ型のヌルポインタ値への暗黙の型変換が存在します。std::nullptr_t 型の値およびマクロNULLを含む任意のヌルポインタ定数にも同様の変換が存在します。
[編集] キーワード
[編集] 例
nullptr がリテラルでなくなっても、ヌルポインタ定数の意味を保持していることを示します。
このコードを実行
#include <cstddef> #include <iostream> template<class T> constexpr T clone(const T& t) { return t; } void g(int*) { std::cout << "Function g called\n"; } int main() { g(nullptr); // Fine g(NULL); // Fine g(0); // Fine g(clone(nullptr)); // Fine // g(clone(NULL)); // ERROR: non-literal zero cannot be a null pointer constant // g(clone(0)); // ERROR: non-literal zero cannot be a null pointer constant }
出力
Function g called Function g called Function g called Function g called
[編集] 参照
- C++23標準 (ISO/IEC 14882:2024)
- 7.3.12 ポインタ変換 [conv.ptr]
- C++20 standard (ISO/IEC 14882:2020)
- 7.3.12 ポインタ変換 [conv.ptr]
- C++17 standard (ISO/IEC 14882:2017)
- 7.11 ポインタ変換 [conv.ptr]
- C++14 standard (ISO/IEC 14882:2014)
- 4.10 ポインタ変換 [conv.ptr]
- C++11 standard (ISO/IEC 14882:2011)
- 4.10 ポインタ変換 [conv.ptr]
[編集] 関連項目
| 処理系定義のヌルポインタ定数 (マクロ定数) | |
| (C++11) |
ヌルポインタ リテラル nullptr の型 (typedef) |
| nullptr のC ドキュメント
| |