c++templatesc++11language-lawyerexplicit-specialization

extending namespace std via partial template specialization


As far as I know, we are allowed (with some exceptions that I won't mention here) to "extend" namespace std by totally specializing a std template function such as std::swap, i.e.

namespace std
{
    template<>
    void swap<Foo>(Foo& lhs, Foo& rhs){...}
}

is perfectly valid.

Since C++11, we can now partially specialize functions. I believe that we can then play the same game and extend std via partial specialization, like

namespace std
{
    template<typename T>
    void swap<Foo<T>>(Foo<T>& lhs, Foo<T>& rhs){...}
}

however I am not sure about this and couldn't find the proper explaining section in the standard. Is the code immediately above correct or does it lead to UB?

PS: As @Columbo mentioned in the answer, we cannot partially specialize function templates, not even in C++11/14. For some reason I thought one can do that, I believed it was at least a proposal.


Solution

  • You might mean [namespace.std]/1:

    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 prohibited181.


    181) Any library code that instantiates other library templates must be prepared to work adequately with any user-supplied specialization that meets the minimum requirements of the Standard.

    If partial specializations of function templates are ever introduced, this quote would also implicitly cover them (as it doesn't restrict itself on explicit specialization).