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(示例)
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才可能实现)
原文链接
新书介绍
《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!

















