std :: common_type的目的是什么? [英] What is the purpose of 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?
作为参考,请参见§20.9.7.6N3242中的表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>
。不是指定无限的允许配对流,而是决定委托给一个单独的模板,该模板使用适用于?:
算术-如果的核心语言规则来找到结果。
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屋!