std:: nullptr_t
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic types | |||||||||||||||||||||
| Fixed width integer types (C++11) | |||||||||||||||||||||
| Fixed width floating-point types (C++23) | |||||||||||||||||||||
|
|||||||||||||||||||||
| Numeric limits | |||||||||||||||||||||
| C numeric limits interface | |||||||||||||||||||||
| Runtime type information | |||||||||||||||||||||
|
|||||||||||||||||||||
|
ヘッダーで定義
<cstddef>
|
||
|
using
nullptr_t
=
decltype
(
nullptr
)
;
|
(C++11以降) | |
std::nullptr_t
はnullポインタリテラル
nullptr
の型です。これはポインタ型でもメンバポインタ型でもない独自の型です。この型のprvalueは
nullポインタ定数
であり、任意のポインタ型およびメンバポインタ型に
暗黙変換
可能です。
sizeof ( std :: nullptr_t ) は sizeof ( void * ) と等しい。
注記
C++標準は、
<stddef.h>
が
<cstddef>
の内容をグローバル名前空間に配置することを要求しており、それにより
<stddef.h>
がインクルードされた際に
nullptr_t
がグローバル名前空間で利用可能であることを要求します。
nullptr_t
はC23までC言語の一部ではありません。
std::nullptr_t
の宣言が他の標準ライブラリヘッダで利用可能かどうかは未規定です。実装は、たとえば
std::nullptr_t
の代わりに
decltype
(
nullptr
)
を使用するなどして、規格が
std::nullptr_t
の使用を要求する場合でも、この名前の導入を回避することができます。
例
2つ以上のオーバーロードが異なるポインタ型を受け取る場合、
std::nullptr_t
のオーバーロードが必要であり、これによってnullポインタ引数を受け取ることが可能になります。
#include <cstddef> #include <iostream> void f(int*) { std::cout << "Pointer to integer overload\n"; } void f(double*) { std::cout << "Pointer to double overload\n"; } void f(std::nullptr_t) { std::cout << "null pointer overload\n"; } int main() { int* pi{}; double* pd{}; f(pi); f(pd); f(nullptr); // would be ambiguous without void f(nullptr_t) // f(0); // ambiguous call: all three functions are candidates // f(NULL); // ambiguous if NULL is an integral null pointer constant // (as is the case in most implementations) }
出力:
Pointer to integer overload Pointer to double overload null pointer overload
関連項目
| nullptr (C++11) | ヌルポインタ値を指定するポインタリテラル |
|
実装定義のヌルポインタ定数
(マクロ定数) |
|
|
(C++11)
(
DR*
)
|
型が
std::nullptr_t
かどうかをチェックする
(クラステンプレート) |
|
Cドキュメント
for
nullptr_t
|
|