可以联合模板吗? [英] Can union be templated?
问题描述
似乎联合可以在c ++ 11中模板化,它们用于例如 std :: optional的引用实现。
在c ++ 11之前是否可能?
是的,似乎这是一直允许的。联合是一个类,模板是一个函数或类模板。
标准的相关部分:
-
[temp]
<...>
/ blockquote> -
[class]
>是用类键
定义的类
(所以人们可能会认为新类型特征 std :: is_class
是一个小小的错误; traits应该分割类型的空间,因此 is_union
是一个单独的,互相排斥的特质。)
It seems unions can be templated in c++11, they are used for example in the reference implementation of std::optional.
Was that possible before c++11 ?
Yes, it seems that this has always been allowed. A union is a class, and a template is either a function or a class template.
Relevant parts of the standards:
[temp]
The declaration in a template-declaration shall
— declare or define a function or a class, [...]
[class]
A union is a class defined with the class-key
union
(So one might argue that the new type trait std::is_class
is a slight misnomer; the traits are supposed to partition the space of types, and so is_union
is a separate, mutually exclusive trait.)
这篇关于可以联合模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!