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
5
votes
2 answers

Can someone explain the type syntax used in this OCaml program?

The types below are taken from this question (* contains an error, later fixed by the OP *) type _ task = | Success : 'a -> 'a task | Fail : 'a -> 'a task | Binding : (('a task -> unit) -> unit) -> 'a task | AndThen : ('a -> 'b task) * 'a task -> 'b…
Mulan
  • 129,518
  • 31
  • 228
  • 259
5
votes
1 answer

Compiler requesting c++17 support to be enabled for std::variant by using the -std++17 flag when -std=c++17 is in compiler output

I had issues a while back getting std::variant to work in a QtCreator project, facing similar complaints here: Can't use c++17 features using g++ 7.2 in QtCreator I resolved this issue, and have been happily working on this project for some time…
Iron Attorney
  • 1,003
  • 1
  • 9
  • 22
5
votes
4 answers

Missing javaCompileTask for variant

Trying to build something with Android Studio 3.0 that worked fine in a previous version. Now I am seeing: Error:Execution failed for task ':mobile-app:transformClassesWithRetrolambdaForDevDebug'. Missing javaCompileTask for variant: dev/debug/0…
user443654
  • 821
  • 1
  • 7
  • 21
5
votes
3 answers

Can a Variant property have default value?

I've written a component who has a Variant property for which I would like to set a default value. TMyComponent = class(TComponent) private FVariantValue : Variant; published property VariantValue : Variant read FVariantValue write FVariantValue…
Fabrizio
  • 7,603
  • 6
  • 44
  • 104
5
votes
2 answers

VBA: difference between Variant/Double and Double

I am using Excel 2013. In the following code fragment, VBA calculates 40 for damage: Dim attack As Variant, defense As Variant, damage As Long attack = 152 * 0.784637 defense = 133 * 0.784637 damage = Int(0.5 * attack / defense * 70) If the data…
ltleelim
  • 51
  • 1
  • 3
5
votes
3 answers

What is the C++ way of handling a framework to call functions of different types from a script?

I have tried different approaches and asked several specific questions regarding sub-problems of my requirement here. But my solution doesn't really work as expected, so I am taking a step back and ask here from a more general perspective. Please…
Matthias
  • 9,817
  • 14
  • 66
  • 125
5
votes
1 answer

usage differences between _variant_t, COleVariant, CComVariant, and VARIANT and using SAFEARRAY variations

I am investigating several Visual Studio 2015 C++ project types which use ADO to access an SQL Server database. The simple example performs a select against a table, reads in the rows, updates each row, and updates the table. The MFC version works…
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
5
votes
1 answer

"Dim myarray() as String" VS "Dim myarray() as Variant"

I'm always afraid to declare things as just Variants under the assumption that an unnecessary large amount of memory will be allocated. Recently working to improve performance of a spreadsheet I however got the opposite impression (see edit below):…
BuckTurgidson
  • 289
  • 3
  • 8
  • 17
5
votes
3 answers

Does VBScript have a DateTime.TryParse equivalent?

Given a variant, does VBScript have an equivalent of C#'s DateTime.TryParse method?
Jim G.
  • 15,141
  • 22
  • 103
  • 166
5
votes
2 answers

SCons: How to use the same builders for multiple variants (release/debug) of a program

The SCons User Guide tells about the usage of Multiple Construction Environments to build build multiple versions of a single program and gives the following example: opt = Environment(CCFLAGS = '-O2') dbg = Environment(CCFLAGS = '-g') o =…
OK.
  • 2,374
  • 2
  • 17
  • 20
5
votes
1 answer

Why can't the compiler find this operator<< overload?

I'm trying to write overloads of operator<< for specific instantiations of standard library containers that will be stored in a boost::variant. Here's a small example that illustrates the problem: #include #include std::ostream…
5
votes
5 answers

type inference var

Type inference makes use of the var keyword. The compiler "infers" what the type of the variable is by what the variable is initialized to. e.g. var somenum=o; becomes int somenum=0; Even though somenum is never declared as being an int, the…
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
5
votes
1 answer

Generic function to convert variant SAFEARRAY to STL containers

I have some functions that I use to convert a 2D variant SAFEARRAY into various STL containers, kinda like so (illustrative only) template std::set SetFromSafeArray(VARIANT srcArray) { CComSafeArray
Sparkles
  • 255
  • 3
  • 8
5
votes
1 answer

Why does assigning a NIL array to a Variant cause a non-empty array to be returned in Delphi 6?

Consider the code below which compiles and runs without error in Delphi 6. When I recover the dynamic string array, instead of seeing an empty array in sa, I see an array with a length of 1 with a single element containing an empty string. Why is…
Robert Oschler
  • 14,153
  • 18
  • 94
  • 227
4
votes
2 answers

How do I make a simplified version of boost::variant?

I have a much simpler set of requirements and do not need much of variant's machinery. I also do not want to depend on boost if I can help it. I need to store either an arbitrary type known at compile time (that may be void). It is either move…
Omnifarious
  • 54,333
  • 19
  • 131
  • 194