# Extending the namespace `std`

From cppreference.com
< cpp‎ | language

### Adding declarations to `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};
        }
    }

### Adding template specializations

It is allowed to add template specializations for any standard library template to the namespace `std` only if the declaration depends on a user-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited.

    // Specialize std::hash so that MyType can be used as a key in
    // std::unordered_set and std::unordered_map
    namespace std {
        template <typename T> struct hash;
        template <> struct hash<MyType> {
          std::size_t operator()(const MyType& t) { return t.hash(); }
        }
    }

- 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

- 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

- None of the type traits defined in `<type_traits>` may be specialized for a user-defined type, except for std::common_type.

- Specializing the template std::complex for any type other than float, double, and long double is unspecified.

- Specializations of std::hash for user-defined types must satisfy `Hash` requirements.

- Specializations of std::atomic must have a deleted copy constructor, a deleted copy assignment operator, and a constexpr value constructor.

- Specializations of std::shared_ptr and std::weak_ptr must be `CopyConstructible` and `CopyAssignable`. In addition, specializations of std::shared_ptr must be `LessThanComparable`, and convertible to bool.

- Specializations of std::istreambuf_iterator must have a trivial copy constructor, a constexpr default constructor, and a trivial destructor.

- Specializations of std::numeric_limits must define all members declared `static constexpr` in the primary template, in such a way that they are usable as integral constant expressions.

- std::unary_function and std::binary_function may not be specialized.

### Explicit instantiation of templates

It is allowed to explicitly instantiate a template defined in the standard library only if the declaration depends on the name of a user-defined type and the instantiation meets the standard library requirements for the original template.

| This section is incomplete Reason: mini-example

### Other restrictions

The namespace `std` may not be declared as an `inline` namespace.

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/language/extending_std&oldid=90197"
Category:

- Todo with reason