Overloading (non-specialized) patterns in the std namespace

This is very pedantic, but in C ++ 03 it clearly did not match the program to overload (not specialize ) the template function in std namespace: see the mention of this here and a lengthy discussion on comp.lang.C ++. Moderated

i.e. it was ok:

 namespace std { template <> void swap (Foo & f, Foo & g) { // ... } } 

but it wasn’t (if I understood correctly ...):

 namespace std { template <typename T> void swap (TempateFoo<T> & f, TempateFoo<T> & g) { // ... } } 

Is this still true in C ++ 11? Also, does this also apply to template classes (e.g. std::hash ) or just to template functions?

EDIT: is there also any example of a standard library implementation so that the latter can disrupt the work in practice? And if not, is there a specific reason for not overloading, as in the second case above? (what could potentially break in theory?)

+7
source share
2 answers

It is not possible to define a partial specialization of a function template in C ++, so your second example defines overloading, not specialization. Because the standard allows you to add specializations to the std , your overloading is illegal.

Is this still true in C ++ 11?

Yes.

Also, is this applicable to template classes (e.g. std :: hash) or just to template functions?

In any case, you cannot overload class templates; you can only overload functions. The same rules apply, however, you can only specialize in a class template if the specialization depends on a custom (i.e. non-standard) type.

Is there a specific reason for not overloading, as in the second case above? (what could potentially break in theory?)

As one example, an implementation may want to take the address of the function, but if you overload the function, then using the address can cause ambiguity and not compile, that is, you just violated the valid code in the standard library.

+7
source

n3376 17.6.4.2.1

The behavior of a C ++ program is undefined if it adds declarations or definitions to the std namespace or to the namespace in the std namespace, unless otherwise specified. A program can add a template specialization for any standard library template for the std namespace only if the declaration is user-defined and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

17.6.4.2.2

C ++ program behavior is undefined if it declares

- explicit specialization of any member function of the standard library class template or

- explicit specialization of any member function template of a standard library class or class template, or

- The explicit or partial specialization of any element class template of a standard library class or template class.

A program can explicitly create a template defined in the standard library only if the declaration depends on the name of the user-defined type and the instance meets the standard library requirements for the original template.

+6
source

All Articles