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
4
votes
3 answers

Sending and receiving arrays over COM

What is the right way to receive and send arrays over COM? Here's my attempt so far: a safearray of doubles wrapped in a variant. //takes variant holding safearray of doubles //returns a similar variant having multipled every element by…
Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79
4
votes
1 answer

Android Studio: No variants found for ':app'

I am working on my final project and I wanted to do project with android studio, I'm really new to android studio, i encountered an error saying No variants found for ':app' I've tried to change or download another version of the SDK, but it solves…
4
votes
2 answers

Does boost::variant work with std::string?

I've written a simple program in C++ with use of boost::variant. Program's code is presented below. #include #include #include int main (int argc, char** argv) { …
stilz
  • 113
  • 1
  • 8
4
votes
1 answer

unique_ptr inside a variant. Is it safe to use?

Id like to ask, is safe having a variant like this? struct A { unique_ptr anything; }; struct B { int x = 0; int y = 0; }; variant myVar; myVar = ... A object; myVar = ... B object; myVar = ... another A object; Would the…
4
votes
1 answer

How to access compatible std::variant variants?

#include struct A { void foo(){} }; struct B { void foo(){} }; int main() { std::variant< A, B > v{ A{} }; v.foo(); // doesn't work } How do I use the std::variant value not knowing it's type but knowing it's…
Vorac
  • 8,726
  • 11
  • 58
  • 101
4
votes
2 answers

How to support implicit conversion from a Variant type, e.g. from int to unsigned long?

I'm trying to have my Variant type, a wrapper around c++17's std::variant, implicitly convert between types where appropriate. For instance, a char to std::string, or int to unsigned long. Here's my code: #include using variant_t =…
CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
4
votes
2 answers

Nested variant updating and deleting in snowflake

Currently streaming Change Data Capture events from MongoDB into snowflake, would like to apply them to the raw data that is already there. Let's say I have a table like this: +---------------------+-----------------+-----------+ | key …
ElvisElvis
  • 160
  • 1
  • 12
4
votes
1 answer

Get std::variant types at compile time

Suppose we have defined somewhere: using mytype_t = std::variant; I want to retrieve all possible types that coud be stored by mytype_t. I have checked the reference. There is no member types defined that I can use (something like:…
4
votes
3 answers

How can I print map key/value with std::variant?

I'm trying to print the key of a certain value in my dictionary. I defined my map like this: std::map> kwargs; kwargs["interface"] = "probe"; kwargs["flag"] = true; kwargs["Height"] = 5;…
user13549804
4
votes
3 answers

Prefer std::string in std::variant for const char *

Consider the following code - #include #include int p(std::variant v) { return v.index(); } int main() { return p("ad"); } instead of choosing std::string, p will be instantiated with variant…
4
votes
4 answers

Default constructing an std::variant from index

What's the easiest way to default construct an std::variant from the index of the desired type, when the index is only known at runtime? In other words, I want to write: const auto indx = std::variant{someobject}.index(); //...somewhere…
Timo
  • 739
  • 1
  • 6
  • 13
4
votes
3 answers

Shopify Storefront API: Getting referenced variant with GraphQL

I'm using the Shopify Storefront API and Accentuate to try to get my hands on a specific variant, but it won't work for me. THE SHORT VERSION: When I select a variant on the website, I get the url: (... url ...)?variant=31696763027492. How do I get…
4
votes
2 answers

How does the caller know when there's a Decimal inside a VARIANT?

The COM VARIANT type is defined using the tagVARIANT structure like this: typedef struct tagVARIANT { union { struct { VARTYPE vt; WORD wReserved1; WORD wReserved2; WORD wReserved3; union { …
Greedo
  • 4,967
  • 2
  • 30
  • 78
4
votes
1 answer

variant of functions with different return types

The code below does not compile: #include #include int main() { using ret_void = std::function; using ret_int = std::function; std::variant var; var.emplace([](){ return 1; }…
Tes
  • 349
  • 3
  • 12
4
votes
3 answers

Conversion system for variants

I have written a variant class, which will be used as the main type in a dynamic language, that will ultimately allow 256 different types of value (header is an unsigned byte, only 20 are actually used). I now want to implement casting/converting…