what is variant member in c++?

前端 未结 3 859
囚心锁ツ
囚心锁ツ 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 00:55

    A variant is a structure containing a union member and an unsigned integer member that describes which member of the union is currently being used. If you don't know what a union is, read about it first, and then come back.

    0 讨论(0)
  • 2021-02-20 01:09

    The term variant is usually employed to identify a member that can hold a value of a set of different types. Similar to a union in the language, the term variant is usually reserved for types that allow the storage of the different options in a type safe way.

    You might want to read over the documentation of the boost variant library for one such example, and if that does not clear up the concept, drop a comment/create a question with your doubts.

    Boost Variant

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题