前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则T.47:避免使用通用名称的高度不受限模板

C++核心准则T.47:避免使用通用名称的高度不受限模板

作者头像
面向对象思考
发布2020-09-10 10:45:17
4380
发布2020-09-10 10:45:17
举报

T.47: Avoid highly unconstrained templates with common names

T.47:避免使用通用名称的高度不受限模板

Reason(原因)

An unconstrained template argument is a perfect match for anything so such a template can be preferred over more specific types that require minor conversions. This is particularly annoying/dangerous when ADL is used. Common names make this problem more likely.

不受限的模板参数会完美匹配任何东西,因此这样的模板可以覆盖需要轻微转换的特定类型。当使用ADL时,这种情况很麻烦/危险。通用的名称会让这个问题更容易发生。

Example(示例)

namespace Bad {
    struct S { int m; };
    template<typename T1, typename T2>
    bool operator==(T1, T2) { cout << "Bad\n"; return true; }
}

namespace T0 {
    bool operator==(int, Bad::S) { cout << "T0\n"; return true; }  // compare to int

    void test()
    {
        Bad::S bad{ 1 };
        vector<int> v(10);
        bool b = 1 == bad;
        bool b2 = v.size() == bad;
    }
}

This prints T0 and Bad.

代码会打印T0和Bad。

Now the == in Bad was designed to cause trouble, but would you have spotted the problem in real code? The problem is that v.size() returns an unsigned integer so that a conversion is needed to call the local ==; the == in Bad requires no conversions. Realistic types, such as the standard-library iterators can be made to exhibit similar anti-social tendencies.

现在Bad中的==被设计用于引发问题,但是你能定位到实际代码中的问题么?问题是v.size()返回一个无符号整数,因此调用本地==时需要转换;Bad中的==则不需要转换。实际的类型,例如标准库中的迭代器等有可能会表现出这种类似反社会问题的倾向。

Note(注意)

If an unconstrained template is defined in the same namespace as a type, that unconstrained template can be found by ADL (as happened in the example). That is, it is highly visible.

如果不受限模板被定义在类型相同的命名空间,这个不受限模板可以被ADL发现(就像示例代码中发生的那样。)。也就是说,它是高度可见的。

Note(注意)

This rule should not be necessary, but the committee cannot agree to exclude unconstrained templated from ADL.

本规则应该是没有必要的,但是委员会不能同意将非受限模板从ADL中排除出去。

Unfortunately this will get many false positives; the standard library violates this widely, by putting many unconstrained templates and types into the single namespace std.

不幸的是,这会引发很多假阳性;标准库将很多非受限模板放入std命名空间,这导致大量违反本规则的情况。

Enforcement(实施建议)

Flag templates defined in a namespace where concrete types are also defined (maybe not feasible until we have concepts).

标记定义在和具体类型定义在同一命名空间的模板(也许只有通过concept才可能实现)

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t47-avoid-highly-visible-unconstrained-templates-with-common-names

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • T.47: Avoid highly unconstrained templates with common names
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档