std
It is undefined behavior to add declarations or definitions to namespace std
or to any namespace nested within std
, 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 namespace std
only if the declaration depends on at least one program-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<> struct std::hash<MyType> { std::size_t operator()(const MyType& t) const { return t.hash(); } };
It is undefined behavior to declare a full or partial specialization of any member class template of a standard library class or class template.
[edit] Function templates and member functions of templatesIt is allowed to add template specializations for any standard library function template to the namespace std
only if the declaration depends on at least one program-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited.
It is undefined behavior to declare a full specialization of any standard library function template.
(since C++20)It is undefined behavior to declare a full specialization of any member function of a standard library class template:
It is undefined behavior to declare a full specialization of any member function template of a standard library class or class template:
[edit] Variable templatesIt is undefined behavior to declare a full or partial specialization of any standard library variable template, except where explicitly allowed.
(since C++14) [edit] Explicit instantiation of templatesIt is allowed to explicitly instantiate a class (since C++20)template defined in the standard library only if the declaration depends on the name of at least one program-defined type and the instantiation meets the standard library requirements for the original template.
[edit] Other restrictionsThe namespace std
may not be declared as an inline namespace.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 120 C++98 users could explicitly instantiate standardRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4