在C ++ 20中将不再允许在std中针对程序定义类型的功能模板进行专业化处理吗? [英] Will specialization of function templates in std for program-defined types no longer be allowed in C++20?

查看:109
本文介绍了在C ++ 20中将不再允许在std中针对程序定义类型的功能模板进行专业化处理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cppreference.com 引用:

添加模板专业化

仅当声明依赖于至少一个程序定义的类型并且该专业满足原始模板的所有要求,除非禁止此类专业.

It is allowed to add template specializations for any standard library |class (since C++20)| template to the namespace std only if the declaration depends on at least one program-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited.

这是否意味着,从C ++ 20开始,将不再允许将功能模板的特殊化添加到用户定义类型的std名称空间中?如果是这样,则意味着许多现有代码可能会中断,不是吗? (在我看来,这是一种激进的"改变.)此外,它将在代码中注入未定义的行为,不会触发编译错误(希望能发出警告).

Does it mean, that starting from C++20, adding specializations of function templates to the std namespace for user-defined types will be no longer allowed? If so, it implies that many pieces of existing code can break, doesn't it? (It seems to me to be kind-of a "radical" change.) Moreover, it will inject into such codes undefined behavior, which will not trigger compilations errors (warnings hopefully will).

推荐答案

目前,它确实具有这种外观.以前包含[namespace.std]

As it stands now it definitly looks that way. Previously [namespace.std] contained

仅当声明依赖于用户定义的类型并且该专业化满足原始模板的标准库要求时,程序才可以向名称空间std添加任何标准库模板的模板专业化.没有明确禁止.

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.

当前的草稿状态

除非明确禁止,否则程序可以向标准命名空间std添加任何标准库类模板的模板专业化,条件是(a)所添加的声明取决于至少一个程序定义的类型,并且(b )的专业化符合原始模板的标准库要求.

Unless explicitly prohibited, a program may add a template specialization for any standard library class template to namespace std provided that (a) the added declaration depends on at least one program-defined type and (b) the specialization meets the standard library requirements for the original template.

强调我的

它看起来像论文您不能由Walter E. Brown专门负责std功能模板!.他在其中详细说明了应对此进行更改的多种原因,例如:

And it looks like the paper Thou Shalt Not Specialize std Function Templates! by Walter E. Brown is responsible for it. In it he details an number of reason why this should be changed such as:

  • Herb Sutter:专业不参与超载. [...]如果您要自定义功能库模板并希望 定制以参与重载解决方案(或者始终是 用于完全匹配),使其成为普通的旧函数,而不是 专业化.而且,如果您确实提供了重载,请避免同时提供 专业化."
  • David Abrahams:使用函数模板专业化是错误的,因为它以不良的方式与重载交互. [...] 例如, 如果您将常规std::swap专用于std::vector<mytype>&, 您的专业不会超过标准向量 特定的swap,因为在此期间不考虑专业化 重载分辨率."
  • Howard Hinnant:这个问题已经解决了很长时间. . . .自行忽略Dave在此方面的专家意见/答案 危险."
  • Eric Niebler:(由于)C ++解析模板中函数调用的方式非常奇怪. . . ,[w]拨打了不合格的电话 swap为了找到可能在[...]中定义的重载 关联的名称空间[...],我们执行using std::swap以便 不存在这种过载的机会,我们找到了默认值 在std名称空间中定义的版本."
  • 高完整性C ++编码标准:重载解析未考虑功能模板的显式专业化.仅有的 在重载解决方案选择了功能模板之后, 明确的专业领域."
  • Herb Sutter: "specializations don’t participate in overloading. [...] If you want to customize a function base template and want that customization to participate in overload resolution (or, to always be used in the case of exact match), make it a plain old function, not a specialization. And, if you do provide overloads, avoid also providing specializations."
  • David Abrahams: "it’s wrong to use function template specialization [because] it interacts in bad ways with overloads. [...] For example, if you specialize the regular std::swap for std::vector<mytype>&, your specialization won’t get chosen over the standard’s vector specific swap, because specializations aren’t considered during overload resolution."
  • Howard Hinnant: "this issue has been settled for a long time. . . . Disregard Dave’s expert opinion/answer in this area at your own peril."
  • Eric Niebler: "[because of] the decidedly wonky way C++ resolves function calls in templates. . . , [w]e make an unqualified call to swap in order to find an overload that might be defined in [...] associated namespaces[...] , and we do using std::swap so that, on the off-chance that there is no such overload, we find the default version defined in the std namespace."
  • High Integrity C++ Coding Standard: "Overload resolution does not take into account explicit specializations of function templates. Only after overload resolution has chosen a function template will any explicit specializations be considered."

这篇关于在C ++ 20中将不再允许在std中针对程序定义类型的功能模板进行专业化处理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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