extending namespace std via partial template specialization

若如初见. 提交于 2020-01-24 12:13:09

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/28077592/extending-namespace-std-via-partial-template-specialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!