C++怎么避免使用通用名称的高度不受限模板
本篇内容主要讲解"C++怎么避免使用通用名称的高度不受限模板",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C++怎么避免使用通用名称的高度不受限模板"吧!
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
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 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才可能实现)
到此,相信大家对"C++怎么避免使用通用名称的高度不受限模板"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!