Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is variant member in c++?

Tags:

c++

c++11

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 ?

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.
like image 739
Whoami Avatar asked Jul 17 '12 15:07

Whoami


People also ask

What is a variant in C++?

(since C++17) The class template std::variant represents a type-safe union. An instance of std::variant at any given time either holds a value of one of its alternative types, or in the case of error - no value (this state is hard to achieve, see valueless_by_exception).

What is boost :: variant?

Boost. Variant, part of collection of the Boost C++ Libraries. It is a safe, generic, stack-based discriminated union container, offering a simple solution for manipulating an object from a heterogeneous set of types in a uniform manner.

Where can I use std variant?

union s save memory because they allow a single piece of memory to be used for different types of objects at different times. Consequently, they can be used to save memory when we have several objects that are never used at the same time. std::variant uses the memory similar to the union .

How do I print a variant?

To print a variant, enter its name on the initial screen of the variant maintenance tool, select either Attributes or Values in the sub-objects group box and choose Print. Note that you cannot print the values if you are working in change mode. The Print Screen List screen appears.


2 Answers

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

like image 85
Steve Jessop Avatar answered Oct 24 '22 13:10

Steve Jessop


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

like image 24
David Rodríguez - dribeas Avatar answered Oct 24 '22 13:10

David Rodríguez - dribeas