c++ - 在 C++20 中是否不再允许在 std 中为程序定义类型专门化函数模板?

标签 c++ std template-specialization function-templates c++20

引自 cppreference.com :

Adding template specializations

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 命名空间?如果是这样,这意味着许多现有代码可能会中断,不是吗? (在我看来,这是一种“激进”的改变。)此外,它会在此类代码中注入(inject)未定义的行为,这不会触发编译错误(希望会发出警告)。

最佳答案

就目前而言,它肯定是那样的。以前包含 [namespace.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.

the current draft states

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.

强调我的

它看起来像论文 Thou Shalt Not Specialize std Function Templates!由 Walter E. Brown 负责。在其中,他详细说明了应更改此设置的一些原因,例如:

  • 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++ - 在 C++20 中是否不再允许在 std 中为程序定义类型专门化函数模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52760580/

相关文章:

c++ - Qt/OO 最佳实践 : connecting signals and slots

c++ - 为什么 srand(time(NULL)) 即使我反复重置它也能顺利工作?

c++ - std::optional 背后的基本原理是什么?

C++ STD find_last_of 不工作?

c++ - 如何在专门针对无参数的可变参数模板类的声明之外定义 C++ 方法?

c++ - 为什么使用 OpenGL 绘制黑色像素而不是透明像素来绘制 8 位纹理?

c++ - 是否有 std::tm 结构的替代方案?

c++ - 静态字段初始化的模板部分特化

c++ - 为什么此代码会出现错误 "template specialization requires ' template< >'"?

c++ - QProcess 保存到 QTextEdit