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
8
votes
1 answer

C++17 std::variant header file (clang 6.0.0)

Update I narrowed down the problem to (probably! it's not entirely clear, even reading all I could find about the topic) that installing stdlibc++-7-dev would provide me with suitable (i.e., C++17-compliant) STL headers and libraries. This (also,…
Marco Massenzio
  • 2,822
  • 1
  • 25
  • 37
8
votes
1 answer

How many types can std::variant define?

I've learned there is a std::variant type in c++17. Looks like there are no predefined data types supported by the variant container but for each variant type the user may define her own data-type set. std::variant v; I wonder, how…
Valentin H
  • 7,240
  • 12
  • 61
  • 111
8
votes
1 answer

Changing active member of union in constant expressions

Playing with constexpr and union I found out, that I can't change active member of an union in constexpr. Just one exception: union of empty classes. constexpr bool t() { struct A {}; struct B {}; union U { A a; B b; } u{}; u.a =…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
8
votes
4 answers

Variant datatype library for C

Is there a decent open-source C library for storing and manipulating dynamically-typed variables (a.k.a. variants)? I'm primarily interested in atomic values (int8, int16, int32, uint, strings, blobs, etc.), while JSON-style arrays and objects as…
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
8
votes
2 answers

Android Studio Gradle - set module build variant

I am setting up project that has a dependancy to one module, and I could successfully make APK file. All I did was adding compile project(':ModuleName') However I am wondering if I can have module dependancy with build variance. So,…
SuperB
  • 83
  • 1
  • 3
8
votes
4 answers

Skip test for specific build variant in Android+Gradle?

I have a specific build variant that is ONLY used for mock testing. I'd prefer not to run unit tests against this variant (but want to run them against other variants). Is there any way to inform gradle to skip unit testing for this specific…
Eric Miles
  • 141
  • 1
  • 6
8
votes
4 answers

How to have a C++ stack with more than one data type?

Here's the problem: I am currently trying to create a simple stack-based programming language (Reverse Polish Notation, FORTH style) as a component of a larger project. I have hit a snag, though. There is no problem with creating a stack in C++ (by…
Stack Tracer
  • 968
  • 7
  • 26
8
votes
3 answers

Tagged unions (aka variant) in C++ with the same type multiple times

I need to create an union, but 2 members of the union would have the same type, thus I need a way to identify them. For example in OCaml : type A = | B of int | C of float | D of float Boost.Variant doesn't seem to support this case, is…
maattdd
  • 181
  • 2
  • 6
8
votes
2 answers

convert a string to a variant in c++

I have this current code, which types "AAPL" into an excel sheet, and the returns the corresponding value. I would like to make it so that after cout << "Ticker: "; i can type in a ticker symbol (such as AAPL) and use this as the variant_t ticker =…
user1594369
  • 127
  • 2
  • 10
7
votes
1 answer

Delphi: No VarIsBoolean( )-function?

In variants.pas, there is several VarIsXXX( )-functions for type-checking a variant. There is no VarIsBoolean( ), though. What's your preferred way of checking if a variant is of type boolean?
Vegar
  • 12,828
  • 16
  • 85
  • 151
7
votes
4 answers

VB6: Disable variants

I have a large VB6 projects where a lot of variables don't have an explicitly defined type, so they automaticly default to Variant type. Finding all those by hand is a massive task, so is there any way to automate this? In VB.Net it's possible to…
Maestro
  • 9,046
  • 15
  • 83
  • 116
7
votes
4 answers

Can an Ada Variant Record be binary compatible to a C++ union?

I am designing a communication middleware for use in an application which has a module in Ada and many modules in C++ which communicates passing parameters (scalar values) and structures. The application runs in MS Windows XP and Windows 7, the C++…
Guarita
  • 173
  • 1
  • 11
7
votes
2 answers

Can a variant array have 0 elements?

The normal dynamic array supports empty (= nil, Length() = 0). The variant array however does not seem to support this. I pass my data in variant array (because of OLE/COM), and I get an error when the user defines 0 elements... I can use varEmpty…
durumdara
  • 3,411
  • 4
  • 43
  • 71
7
votes
1 answer

Variant-type object array element is deallocated by With statement

If an object-array is declared as a Variant type (in order to easily check whether it is initialized using the IsEmpty function) then, if the subsequently defined array's elements are referenced as the object-expression of a With statement (e.g.…
pstraton
  • 1,080
  • 14
  • 9
7
votes
2 answers

Is it necessary to assign a default value to a variant returned from a Delphi function?

Gradually I've been using more variants - they can be very useful in certain places for carrying data types that are not known at compile time. One useful value is UnAssigned ('I've not got a value for you'). I think I discovered a long time ago…
Brian Frost
  • 13,334
  • 11
  • 80
  • 154