C++核心准则T.144:不要特化函数模板​

2020-10-10 09:57:03 浏览数 (1)

T.144: Don't specialize function templates

T.144:不要特化函数模板

Reason(原因)

You can't partially specialize a function template per language rules. You can fully specialize a function template but you almost certainly want to overload instead -- because function template specializations don't participate in overloading, they don't act as you probably wanted. Rarely, you should actually specialize by delegating to a class template that you can specialize properly.

你无法为每条语言规则部分特化函数模板。你可以完全特化函数模板,但是几乎一定想要重载函数--因为函数模板特化不算重载,它们不会像你可能期待的那样动作。极特殊情况下,你应该通过委托给一个你可以正确特化的模板类来实现特化。

Example(示例)

代码语言:javascript复制
???

Exceptions: If you do have a valid reason to specialize a function template, just write a single function template that delegates to a class template, then specialize the class template (including the ability to write partial specializations).

例外:如果你确实有合理的理由特化函数模板,只要写一个单独的函数模板,以便向一个模板类进行委托,然后定义一个模板类(包含实现部分特化的能力)

Enforcement(实施建议)

  • Flag all specializations of a function template. Overload instead. 标记所有函数模板的特化。用重载代替。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t144-dont-specialize-function-templates

0 人点赞