Questions tagged [variant]

A variant data type is a tagged union that holds other data types. It is a standard data type in [ocaml], and typically used for interop calls between languages ([c++] and [vb6]) in classic Microsoft Windows [com] programming. It also exists in other languages using other names, such as [discriminated-unions] or the more general concept of [algebraic-data-types]

In , the variant type is a basic building block of the language. Custom variants are defined with a set of constructors that restrict which types it can hold, and are used to inspect it in and to warn if a pattern match does not cover all constructors.

Questions that should have this tag would include ones involving how to pass data between Windows processes or within the same process using and classic function calls. Often the calls occur between languages (e.g. to ), but they are also used in single language programs (e.g. ) to achieve a plug-in, modular architecture with DLL files.

A variant is technically a structure holding a union of data types along with a VARTYPE that indicates which member of the structure is valid. It can hold any other data type, including primitive types (BYTE, LONG), pointers to primitive types (LONG*, FLOAT*), IDispatch pointers, and even pointers to other variants.

There are a couple of helper classes Microsoft has created to help with the details of dealing with raw variants:

These classes relieve much of the grunt work of interop programming with variants such as doing deep destroys on contained SAFEARRAYs.

Definitions

  • Tagged union: A data structure that can be used to hold a number of different data types, only one of which is valid at a time. There is a field (VARTYPE) which indicates the valid data member (https://en.wikipedia.org/wiki/Tagged_union)

Examples

OCaml

(* Definition of the built-in option type *)
type 'a option =
| Some of 'a
| None

(* Construction *)
let var = Some "hello"

(* Inspection through pattern matching *)
let greeting =
  match var with
  | Some str -> str
  | None -> "howdy"

Visual Basic

Dim var as Variant

var = "hello"   'Variant holds a string

var = 7         'Variant holds an Integer

C++

_variant_t var;

var = "hello";  //Variant holds a string

var = 7;        //Variant holds an int

Resources

Related tags

1152 questions
12
votes
3 answers

How do I represent variants (sum-types) in JSON?

Algebraic data types are a way to accurately describe the data. When it comes to JSON there is no problem with product types, that is each structure is listing props one-by-one that belong to them. However it's not clear what to do with a sum-types…
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
12
votes
14 answers

Function which returns an unknown type

class Test { public: SOMETHING DoIt(int a) { float FLOAT = 1.2; int INT = 2; char CHAR = 'a'; switch(a) { case 1: return INT; case 2: return FLOAT; case 3: return CHAR; } } }; int main(int argc, char* argv[]) { Test obj; …
Jacob
  • 34,255
  • 14
  • 110
  • 165
11
votes
4 answers

What are the pitfalls of using sql_variant?

I've read and heard several times that sql_variant should be avoided. I think I have a great use case for it. I've used varchar(max) in the past to store different types in the same column, but it seems sensible to avoid the de/serialization…
Daniel
  • 47,404
  • 11
  • 101
  • 179
11
votes
1 answer

What should I use instead of void as one of the alternative types in an variant?

I want to have a variant which may contain type Foo, (disjoint) type Bar, or nothing. Well, naturally, I was thinking of using std::variant - but this doesn't seem to work. That is, you can define this type, but if you try to…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
11
votes
2 answers

how to increase the number of types that can handled by boost::variant

I am designing a parser for verilog language, and one of the rule have 25 components, which I need a large boost::variant to hold it: typedef boost::variant< shared_ptr , …
shengyushen
  • 289
  • 3
  • 13
11
votes
2 answers

How to determine if a boost::variant variable is empty?

I have defined a boost::variant var like this: boost::variant foo; This variable, when instantiated but not initialized, has a value of type boost::blank, because boost::blank is the first type passed to the templated…
FerranMG
  • 181
  • 1
  • 11
11
votes
2 answers

Conditionally trivial destructor

Inventing a discriminated union/tagged variant I conclude that there is particular need in such a feature as "make destructor trivial on some conditions at compile time". I mean some kind of SFINAE or something like (pseudocode): template< typename…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
11
votes
4 answers

VARIANT datatype of C++ into C#

What is equivalent of the VARIANT datatype of C++ in C#? I have code in C++ which uses the VARIANT datatype. How can I convert that code in C#?
kamal
  • 739
  • 1
  • 9
  • 21
10
votes
2 answers

What's the difference between and in Java Facelets?

See this. When and why to use , instead of ? I've seen Primefaces won't work with , though.
Lenik
  • 13,946
  • 17
  • 75
  • 103
10
votes
3 answers

std::variant converting constructor doesn't handle const volatile qualifiers

The code below: int i = 1; const int i_c = 2; volatile int i_v = 3; const volatile int i_cv = 4; typedef std::variant TVariant; TVariant var (i ); TVariant…
anton_rh
  • 8,226
  • 7
  • 45
  • 73
10
votes
2 answers

Shopify - Retrieves multiple variant detail by multiple variant id

Is there any way to get multiple variant details using multiple variant id in single shopify call. Actually, I want to get price of all product variant which are listed in order using shopify call. I know following way for get single variant detail…
tejash patel
  • 591
  • 1
  • 7
  • 19
10
votes
1 answer

optimizing of std::visit possible?

While using std::visit / std::variant I see in the profiler output that std::__detail::__variant::__gen_vtable_impl functions take the most time. I did a test like this: // 3 class families, all like this class ElementDerivedN: public ElementBase { …
Klaus
  • 24,205
  • 7
  • 58
  • 113
10
votes
1 answer

Questions about three constructors in std::variant's proposed interface

Why does constructor (4) exist for std::variant from http://en.cppreference.com/w/cpp/utility/variant/variant? It seems like it is going to cause a lot of ambiguity in code that could otherwise have been avoided by being explicit.. For example the…
Curious
  • 20,870
  • 8
  • 61
  • 146
10
votes
3 answers

visitor template for boost::variant

I would like to use a boost.variant as a parameter to a template 'Visitor' class which would provide visitor operators as required by the boost.variant visitor mechanism, in this case all returning void i.e., void operator()(T0…
Tom Jordan
  • 101
  • 1
  • 1
  • 3
10
votes
2 answers

std::any by std::exception_ptr

Probably I am not the first person finding out that std::exception_ptr could be used to implement an any type (performance considerations being put aside), as it is probably the only type in C++ that can hold anything. Googling did not, however,…
JohnB
  • 13,315
  • 4
  • 38
  • 65