通过部分模板专门化扩展名称空间std [英] extending namespace std via partial template specialization

查看:80
本文介绍了通过部分模板专门化扩展名称空间std的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,通过完全专门化std模板函数(例如std::swap)(即namespace std >

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

是完全有效的.

自C ++ 11起,我们现在可以对函数进行部分专业化.我相信我们可以然后玩相同的游戏并通过部分专业化来扩展std,例如

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

但是我对此不确定,也无法在标准中找到适当的解释部分.上面的代码正确无误还是导致UB?

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:正如@Columbo在答案中提到的那样,我们不能部分专门化函数模板,即使在C ++ 11/14中也是如此.由于某种原因,我认为可以做到这一点,我认为这至少是一项建议.

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.

推荐答案

您可能是指[namespace.std]/1:

You might mean [namespace.std]/1:

程序可以为任何标准库添加模板专业化 仅当声明依赖于 用户定义的类型和专业化符合标准库 原始模板的要求,但未明确 禁止 181 .


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)必须实例化其他库模板的任何库代码,以使其能够与用户提供的任何文件充分配合使用 符合标准最低要求的专业化.

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

这篇关于通过部分模板专门化扩展名称空间std的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆