什么是C ++中的变体成员? [英] what is variant member in c++?

查看:137
本文介绍了什么是C ++中的变体成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手。我经常从一些变体成员网站上阅读?

I am new to C++. I read very frequently from some sites that variant member?.

class School
{
  int x; -> data member. 
}

我知道数据成员。但是什么是变体成员?

I know data member. but what is variant member ?

注意:
来自c ++规范:在构造函数页面下。

NOTE: From the c++ specification: Under Constructor page.

X is a union-like class that has a variant member with a non-trivial default constructor.


推荐答案

变体成员的定义为9.5 / 8 C ++ 11:

"Variant member" is defined in 9.5/8 of C++11:


类似工会的类是工会或以匿名工会
为直接成员的类会员。类联合类X具有一组变体成员。
如果X是一个并集,则其变体成员是非静态数据成员;
,否则,其变体成员是属于X的所有
匿名联合的非静态数据成员。

A union-like class is a union or a class that has an anonymous union as a direct member. A union-like class X has a set of variant members. If X is a union its variant members are the non-static data members; otherwise, its variant members are the non-static data members of all anonymous unions that are members of X.

换句话说,联合的所有非静态数据成员都是变量成员,对于包含任何匿名联合的类,其非静态数据成员都是该类的变量成员。

In other words, all the non-static data members of a union are "variant members", and for a class containing any anonymous unions, their non-static data members are "variant members" of the class.

您引用的上下文为12.1 / 5,表示如果类似工会的类的变体成员具有非平凡的默认构造函数,则该类本身的默认构造函数被删除。问题是哪个变体成员应该由该类的默认构造函数构造,而解决方案是不使用默认构造函数。如果所有变体成员都具有琐碎的默认构造函数,则没有问题,因为不执行任何操作,该类的默认构造函数将平等地构造所有/全部。

The context that you quoted is 12.1/5, saying that if a union-like class has a variant member with a non-trivial default constructor, then the default constructor of the class itself is deleted. The problem is which variant member should be constructed by the default constructor of the class, and the solution is not to have a default constructor. If all variant members have trivial default constructors there's no problem, since by doing nothing the default constructor of the class is constructing all/none of them equally.

boost :: variant 是另一回事。如果某些站点说变体成员,当它们表示给定的 boost :: variant 可以容纳的可能类型时,我也不会感到惊讶。说出变量的成员。但这不是C ++ 11标准中新定义的含义。

boost::variant is a separate thing. I wouldn't be too surprised if "some sites" say "variant members" when they mean "the possible types that a given boost::variant can hold", that is to say the "members" of that variant. But that's not the meaning newly-defined in the C++11 standard.

这篇关于什么是C ++中的变体成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆