Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Extending the namespacestd

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       

      Contents

      [edit]Adding declarations tostd

      It is undefined behavior to add declarations or definitions to namespacestd or to any namespace nested withinstd, with a few exceptions noted below.

      #include <utility> namespace std{// a function definition added to namespace std: undefined behavior    pair<int,int> operator+(pair<int,int> a, pair<int,int> b){return{a.first+ b.first, a.second+ b.second};}}

      [edit]Adding template specializations

      [edit]Class templates

      It is allowed to add template specializations for any standard library class template to the namespacestd only if the declaration depends on at least oneprogram-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited.

      // Get the declaration of the primary std::hash template.// We are not permitted to declare it ourselves.// <typeindex> is guaranteed to provide such a declaration,// and is much cheaper to include than <functional>. #include <typeindex> // Specialize std::hash so that MyType can be used as a key in// std::unordered_set and std::unordered_map.  Opening namespace// std can accidentally introduce undefined behavior, and is not// necessary for specializing class templates.template<>structstd::hash<MyType>{std::size_t operator()(const MyType& t)const{return t.hash();}};
      • Specializing the templatestd::complex for any type other thanfloat,double, andlongdouble is unspecified.
      • Specializations ofstd::hash for program-defined types must satisfyHash requirements.
      • Specializations ofstd::atomic must have a deleted copy constructor, a deleted copy assignment operator, and a constexpr value constructor.
      • Specializations ofstd::istreambuf_iterator must have a trivial copy constructor, a constexpr default constructor, and a trivial destructor.
      (since C++11)
      (until C++17)

      It is undefined behavior to declare a full or partial specialization of any member class template of a standard library class or class template.

      This section is incomplete
      Reason: mini-example

      [edit]Function templates and member functions of templates

      It is allowed to add template specializations for any standard library function template to the namespacestd only if the declaration depends on at least oneprogram-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited.

      (until C++20)

      It is undefined behavior to declare a full specialization of any standard library function template.

      (since C++20)
      This section is incomplete
      Reason: mini-example

      It is undefined behavior to declare a full specialization of any member function of a standard library class template:

      This section is incomplete
      Reason: mini-example

      It is undefined behavior to declare a full specialization of any member function template of a standard library class or class template:

      This section is incomplete
      Reason: mini-example

      [edit]Variable templates

      It is undefined behavior to declare a full or partial specialization of any standard library variable template, except where explicitly allowed.

      This section is incomplete
      Reason: mini-example
      (since C++20)
      (since C++14)

      [edit]Explicit instantiation of templates

      It is allowed to explicitly instantiate aclass(since C++20)template defined in the standard library only if the declaration depends on the name of at least oneprogram-defined type and the instantiation meets the standard library requirements for the original template.

      This section is incomplete
      Reason: mini-example

      [edit]Other restrictions

      The namespacestd may not be declared as aninline namespace.

      Addressing restriction

      The behavior of a C++ program is unspecified (possibly ill-formed) if it explicitly or implicitly attempts to form a pointer, reference (for free functions and static member functions) or pointer-to-member (for non-static member functions) to a standard library function or an instantiation of a standard library function template, unless it is designated anaddressable function (see below).

      Following code was well-defined in C++17, but leads to unspecified behaviors and possibly fails to compile since C++20:

      #include <cmath>#include <memory> int main(){// by unary operator&auto fptr0=&static_cast<float(&)(float,float)>(std::betaf); // by std::addressofauto fptr1=std::addressof(static_cast<float(&)(float,float)>(std::betaf)); // by function-to-pointer implicit conversionauto fptr2=static_cast<float(&)(float)>(std::riemann_zetaf); // forming a referenceauto& fref=static_cast<float(&)(float)>(std::riemann_zetaf);}

      Designated addressable functions

      (since C++20)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 120C++98users could explicitly instantiate standard
      library templates for non-user defined types
      prohibited
      LWG 232C++98users could explicitly specialize standard library templates
      if the declaration depends on a user-defined name of
      external linkage (which can refer to a non-user-defined type)
      only allowed for
      user-defined types
      LWG 422C++98users could specialize individual members or member templates
      without specializing the whole standard library class or class template
      the behavior is
      undefined in this case
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/extending_std&oldid=170135"

      [8]ページ先頭

      ©2009-2025 Movatter.jp