gpt4 book ai didi

c++ - c++中的变体成员是什么?

转载 作者:可可西里 更新时间:2023-11-01 14:59:26 29 4
gpt4 key购买 nike

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

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

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

注意:来自 c++ 规范:Under Constructor 页面。

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

最佳答案

“变体成员”在 C++11 的 9.5/8 中定义:

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.

换句话说, union 体的所有非静态数据成员都是“变体成员”,对于包含任何匿名 union 体的类,它们的非静态数据成员是类的“变体成员”。

你引用的上下文是 12.1/5,说如果一个类似 union 的类有一个带有非平凡默认构造函数的变体成员,那么类本身的默认构造函数将被删除。问题是哪个变体成员应该由类的默认构造函数构造,解决方案是不要有默认构造函数。如果所有变体成员都有普通的默认构造函数,那没有问题,因为通过什么都不做,类的默认构造函数正在平等地构造所有/不构造它们。

boost::variant 是一个独立的东西。如果“某些站点”在表示“给定 boost::variant 可以容纳的可能类型”时说“变体成员”,也就是说“成员”,我不会感到太惊讶该变体。但这不是 C++11 标准中新定义的含义。

关于c++ - c++中的变体成员是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11525964/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com