std :: common_type [英] std::common_type
问题描述
我开始看着 std :: common_type
,我不确定其目的和功能。
一些事情仍然让我很奇怪:
I started looking at std::common_type
and am not exactly sure about its purpose and its functionality.
A few things still strike me as odd:
- 参数顺序很重要:
common_type< Foo ,Bar,Baz>
可能不同于common_type< Baz,Foo,Bar>
。任一可能编译,另一个可能不。虽然从common_type
的定义清楚,这感觉奇怪和不直观。这是缺乏普遍的解决方案还是打算? - 实例化可能会导致编译器错误,而不是我可以处理的事情。如何检查
common_type
是否会真正编译?is_convertible
不够,因为common_type
可能是专门的? -
在这种情况下仍然没有办法找出共同类型:
- Order of arguments is important:
common_type<Foo, Bar, Baz>
might be different fromcommon_type<Baz, Foo, Bar>
. Either might compile, the other might not. While this is clear from the waycommon_type
is defined, it feels strange and unintuitive. Is this for lack of a universal solution or intended? - Instantiation can result in a compiler error instead of something I can handle. How to check if
common_type
will actually compile?is_convertible
is not enough ascommon_type
might be specialized? There is still no way to figure out the common type in a situation like this:
struct Baz;
struct Bar { int m; };
struct Foo { int m; };
struct Baz { Baz(const Bar&); Baz(const Foo&); };
推荐的解决方案是专门化 common_type
这是乏味的。是否有更好的解决方案?
The recommended solution would be to specialize common_type
which is tedious. Is there a better solution?
有关参考,请参阅N3242中的§20.9.7.6Table 57。
For reference see §20.9.7.6 Table 57 in N3242.
推荐答案
std :: common_type
被用于 std: :duration
---如果添加 std :: duration< int>
和 std :: duration< short> ;
,那么结果应该是 std :: duration< int>
。不是指定允许的配对的无限流,而是决定委托给一个单独的模板,使用适用于?的核心语言规则找到结果:
arithmetic-if
std::common_type
was introduced for use with std::duration
--- if you add a std::duration<int>
and a std::duration<short>
then the result should be std::duration<int>
. Rather than specifying an endless stream of allowed pairings, the decision was made to delegate to a separate template which found the result using the core language rules applicable to the ?:
arithmetic-if operator.
然后,人们看到这个模板通常很有用,它被添加为 std :: common_type
,并扩展以处理任意数量的类型。在C ++ 0x库中,它只用于成对的类型。
People then saw that this template might be generally useful, and it was added as std::common_type
, and extended to handle an arbitrary number of types. In the C++0x library it is only used for pairs of types though.
你应该能够使用新的SFINAE规则来检测是否有一些实例化 std :: common_type
有效。我没有尝试过。在大多数情况下,如果没有一个共同的类型,那么没有任何有意义的你可以做任何反正,所以编译错误是合理的。
You should be able to use the new SFINAE rules to detect whether or not some instantiation of std::common_type
is valid. I haven't tried though. In most cases if there isn't a "common type" then there isn't anything meaningful you can do anyway, so a compile error is reasonable.
std :: common_type
不是魔术 - 它遵循?:
的规则。如果 true?a:b
将编译, std :: common_type< decltype(a),decltype(b)> :: type
会给你结果的类型。
std::common_type
is not magic --- it follows the rules of ?:
. If true?a:b
will compile, std::common_type<decltype(a),decltype(b)>::type
will give you the type of the result.
这篇关于std :: common_type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!