前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则T.144:不要特化函数模板​

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

作者头像
面向对象思考
发布2020-10-10 09:57:03
2920
发布2020-10-10 09:57:03
举报

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-09-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reason(原因)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档