专栏首页C++核心准则原文翻译C++核心准则T.47:避免使用通用名称的高度不受限模板

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

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

本文分享自微信公众号 - 面向对象思考(OOThinkingDalian),作者:面向对象思考

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-09-09

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C++核心准则C.8:存在非公有成员时,使用class而不是struct定义类

    Readability. To make it clear that something is being hidden/abstracted. This is...

    面向对象思考
  • C++核心准则ES.74:尽量在循环变量初始化表达式中定义循环变量​

    Limit the loop variable visibility to the scope of the loop. Avoid using the loo...

    面向对象思考
  • C++核心准则T.44:使用函数模板推断类模板参数类型(如果可能)

    Writing the template argument types explicitly can be tedious and unnecessarily ...

    面向对象思考
  • Codeforces 839C Journey【DFS】

    C. Journey time limit per test:2 seconds memory limit per test:256 megabytes inp...

    Angel_Kitty
  • Codeforces Round #199 (Div. 2)C. Cupboard and Balloons

    A girl named Xenia has a cupboard that looks like an arc from ahead. The arc is ...

    glm233
  • 如何通过solc编译solidity编写的以太坊智能合约

    solidity编写的以太坊智能合约可通过命令行编译工具solc来进行编译,成为以太坊虚拟机中的代码。solc编译后最终部署到链上形成我们所见到的各种智能合约。

    笔阁
  • spark杂记:Spark Basics

    Spark 学习笔记可以follow这里:https://github.com/MachineLP/Spark-

    MachineLP
  • Baozi Leetcode 190: Reverse Bits & 中年大叔程序员沮丧聊天疫情中的WFH

    哈喽大家好!有段时间没有跟大家更新了,各位疫情期间在家工作是否顺心?欢迎收听包子聊天系列:中年程序员大叔沮丧聊天-疫情中WFH怎么感觉更累了?!

    包子面试培训
  • Node.js 开发模式(设计模式)

    Asynchronous code & Synchronous code As we have seen in an earlier post (here), ...

    庞小明
  • PAT 甲级 1019 General Palindromic Number(简单题)

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制...

    ShenduCC

扫码关注云+社区

领取腾讯云代金券