在标准命名空间中添加模板专业化 [英] Adding template specialization in std namespace

查看:75
本文介绍了在标准命名空间中添加模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我试图回答问题

Background:
I tried to answer the question Why isn't my overloading < operator not working for STL sort. One of my suggestion (apart from using predicate) was to move the custom operator < for std::string in namespace std so that it can be preferred by the compiler over templated version.

以极高的速度,答案由享有盛誉的用户发表以下评论:

At lightening speed the answer was down-voted with following comment from a highly reputed user:

这是未定义的行为,不允许您添加声明 到名称空间std,因为它可以更改标准的行为 库组件

This is undefined behaviour, you are not allowed to add declarations to namespace std because it can change the behaviour of the standard library componens

我的问题:
即使stl类型的声明不包含用户定义的数据类型,也可以为stl类型添加模板专业化吗?

My Question:
Is it okay to add template specialization for stl types even if the declaration of this specialization doesn't contain user defined data type?

p.s.我已删除答案,因为我担心这可能是错误的

推荐答案

C ++ 11,[namespace.std]§1:

C++11, [namespace.std]§1:

如果C ++程序的声明或定义添加到名称空间std或 除非另有说明,否则名称空间std中的名称空间.程序可能会添加模板专业化 仅当声明依赖于用户定义的类型时,才能将任何标准库模板添加到名称空间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.

以上段落明确禁止不依赖于用户定义类型的专业化.

The above paragraph explcitily prohibits specialisations which do not depend on a user-defined type.

关于动机:您不会添加模板专长,而是添加单独的声明,这也是禁止的.

As to the motivation: you wouldn't be adding a template specialisation, but a separate declaration, which is also prohibited.

这篇关于在标准命名空间中添加模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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