In 17.6.4.2.1/1 and 17.6.4.2.1/2 of the current draft standard restrictions are placed on specializations injected by users into namespace std
.
The behavior of a C ++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
I cannot find where in the standard the phrase user-defined type is defined.
One option I have heard claimed is that a type that is not std::is_fundamental
is a user-defined type, in which case std::vector<int>
would be a user-defined type.
An alternative answer would be that a user-defined type is a type that a user defines. As users do not define std::vector<int>
, and std::vector<int>
is not dependent on any type a user defines, std::vector<int>
is not a user-defined type.
A practical problem this impacts is "can you inject a specialization for std::hash
for std::tuple<Ts...>
into namespace std
? Being able to do so is somewhat convenient -- the alternative is to create another namespace where we recursively build our hash for std::tuple
(and possibly other types in std
that do not have hash
support), and if and only if we fail to find a hash in that namespace do we fall back on std
.
However, if this is legal, then if and when the standard adds a hash
specialization for std::tuple
to namespace std
, code that specialized it already would be broken, creating a reason not to add such specializations in the future.
While I am talking about std::vector<int>
as a concrete example, I am trying to ask if types defined in std
are ever user-defined type s. A secondary question is, even if not, maybe std::tuple<int>
becomes a user-defined type when used by a user (this gets slippery: what then happens if something inside std
defines std::tuple<int>
, and you partial-specialize hash
for std::tuple<Ts...>
).
There is currently an open defect on this problem.
int
,char
etc. But I agree that this makes the usage in the context cited here weird. – Konrad Rudolphclass
orstruct
. – Robert Harvey