what is variant member in c++?

前端 未结 3 843
囚心锁ツ
囚心锁ツ 2021-02-20 00:34

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

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-20 01:20

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

    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.

    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 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.

提交回复
热议问题