Restrictions on Namespace std Specializations
Introduction
The std namespace is crucial in C , housing fundamental libraries and templates. However, users may wonder where the line is drawn between permissible and prohibited customizations within this namespace. This article will illuminate the templates that cannot be specialized in std.
Prohibited Templates
According to the C standard, certain templates within std cannot be explicitly specialized. These include:
-
numeric_limits: Prohibited for non-arithmetic standard types (e.g., complex)
-
shared_ptr: Requires CopyConstructibility, CopyAssignability, and LessThanComparability
-
weak_ptr: Requires CopyConstructibility and CopyAssignability
-
std::hash: Specializations must meet the hash class template requirements
-
: undefined behavior to add specializations for any class templates within this subclause (except for limited common_type specializations)
-
Locales: Certain required specializations exist
-
istreambuf_iterator: Requires trivial copy constructor, constexpr default constructor, and trivial destructor
-
complex: undefined effect for instantiating with types other than float, double, or long double
-
atomic: Requires deleted copy constructor, deleted copy assignment operator, and constexpr value constructor
-
unary_function and binary_function: Deprecated; cannot be specialized
General Restrictions
Beyond the prohibited templates, the C standard emphasizes the following restrictions:
- It is undefined behavior to declare or define anything within std or its sub-namespaces unless explicitly specified.
- Specializations can be added to std only if they depend on user-defined types and meet the standard requirements for the original template.
- Explicit specializations of member functions, member function templates, or member class templates of std library classes are prohibited.
- Explicit instantiations are allowed only if they depend on user-defined type names and meet the standard requirements for the original template.
The above is the detailed content of What Templates in the `std` Namespace Cannot Be Specialized in C ?. For more information, please follow other related articles on the PHP Chinese website!