Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::nullptr_t

      From cppreference.com
      <cpp‎ |types
       
       
      Utilities library
       
       
      Defined in header<cstddef>
      using nullptr_t= decltype(nullptr);
      (since C++11)

      std::nullptr_t is the type of the null pointer literalnullptr. It is a distinct type that is not itself a pointer type or a pointer to member type. Prvalues of this type arenull pointer constants, and may beimplicitly converted to any pointer and pointer to member type.

      sizeof(std::nullptr_t) is equal tosizeof(void*).

      [edit]Notes

      The C++ standard requires<stddef.h> to place the contents of<cstddef> in the global namespace, and thereby requiresnullptr_t to be available in the global namespace when<stddef.h> is included.

      nullptr_t is not a part of C until C23.

      It is unspecified whether the declaration ofstd::nullptr_t is available in any other standard library header. An implementation may avoid introducing this name even when the standard requiresstd::nullptr_t to be used, by e.g. spellingdecltype(nullptr) instead.

      [edit]Example

      If two or more overloads accept different pointer types, an overload forstd::nullptr_t is necessary to accept a null pointer argument.

      Run this code
      #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)}

      Output:

      Pointer to integer overloadPointer to double overloadnull pointer overload

      [edit]See also

      nullptr(C++11) the pointer literal which specifies a null pointer value[edit]
      implementation-defined null pointer constant
      (macro constant)[edit]
      (C++11)(DR*)
      checks if a type isstd::nullptr_t
      (class template)[edit]
      C documentation fornullptr_t
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/nullptr_t&oldid=174454"

      [8]ページ先頭

      ©2009-2025 Movatter.jp