Questions tagged [variant-type]

A data type or a class designed to emulate a data type that holds a single value of a type that is either from an explicitly (compile-time) specified list, or arbitrary. The actual type of the stored value is only known at runtime.

Filter by
Sorted by
Tagged with
-2 votes
1 answer
57 views

C++ compile time polymorphism example [closed]

I found an example using std::variant to provide compile time polymorphism as a possible optimisation to runtime polymorphism in 'Software Architecture Using C++', https://github.com/PacktPublishing/...
arcomber's user avatar
  • 2,449
5 votes
1 answer
130 views

A parse function for text line with fields separated by a comma with the known number and their type, simplified version

simplifying this question, taking most of the comment into considerations, I ended up with this version, looking for some feedback ...
G. Nass's user avatar
  • 133
4 votes
2 answers
152 views

Transpose types variadicly

I want to do template metaprogramming to compute the conversion from std::variant<Ts...> to ...
Tom Huntington's user avatar
6 votes
2 answers
543 views

C++ Minimal CSV parser cut2

Looking at this questoin, I was inspired by the idea and the approach, so I tried to write a second version of the code, taking most of the comments into considerations. Please note that I am still ...
Libra's user avatar
  • 71
3 votes
1 answer
358 views

C++: Visit Any Type Holding Other Types (`std::any`, `std::variant`, etc.)

Intro I want to visit the value held by a class, which could be of multiple types. This class is very similar to a std::any, so I will be using this in this ...
mfacchinelli's user avatar
10 votes
3 answers
3k views

Dynamic type implementation in C

Dynamic Type - [ int, char, bool, ... => dtype ] Dynamic Type in C, A project that I had been thinking of for a while and made it. How it works ? Dtype currently is same as ...
Pear's user avatar
  • 243
1 vote
1 answer
266 views

Helper functions for use with std::variant without an actual object

Sometimes, you want to an type of a particular index in an std::variant, and do something with that type without having an actual object. An example use-case would be de-serialization. What do you ...
user877329's user avatar
4 votes
1 answer
654 views

Haskell: implementing Read for a custom dynamic value type

I'm using Haskell to interpret a dynamically-typed language. I have a sum type called Value which can represent some basic objects: ...
pxeger's user avatar
  • 143
3 votes
1 answer
1k views

visit for std::any

While solving an online excersise I have written some basic implementation for a visit function that works with std::any. The ...
Sergey Kolesnik's user avatar
4 votes
1 answer
498 views

Token class implementation with std::variant

So I've been trying to implement a Token class in C++. At first I wanted to use a simple enum class to store the Kind of the ...
Famiu's user avatar
  • 196
4 votes
2 answers
818 views

Type agnostic, statically allocated, thread-safe queue in C

I need a queue in C for an embedded application in which I cannot allocate memory dynamically. I avoided using void* for the queue elements (which is not convenient ...
Sparkler's user avatar
  • 309
3 votes
1 answer
418 views

C++ My implementation of a simple sum type (variant)

Here is a simple variant type I have made. For simplicity, it only can handle two different values. I am planning on using this to create a variadic variant template, using inheritance and recursion. ...
Blue7's user avatar
  • 373
6 votes
5 answers
361 views

Simplify & Reduce steps in the IsDistinct function

Because I often have to deal with two Variants that may or may not be Null, we need a null-safe equality test, so I came up with ...
this's user avatar
  • 2,019
13 votes
6 answers
922 views

Assign a variant to a variant

Quick review: I've been writing API code which receives user input that can come as either an object or a value type - a problem outlined here. In order to write an unknown data type to a variable, ...
Greedo's user avatar
  • 2,345
11 votes
2 answers
264 views

YAAI (Yet another any implementation)

I am a C# game developer currently learning C++ and this is my second big-ish project (the first one being a vector implementation). This time I opted for implementing a mot-a-mot ...
Marius T's user avatar
  • 381
6 votes
1 answer
274 views

C++17 Either implementation for error handling [closed]

This is a C++17 Either implementation for error handling. First intent is I want to improve myself. Second intent is I want to create a more expressive solution than variant for exception free error ...
István Simon's user avatar
22 votes
2 answers
4k views

Implementation of std::any

Today, I decided to implement std::any using the cppreference page. I've never actually used std::any before and after seeing ...
Indiana Kernick's user avatar
3 votes
0 answers
937 views

Type erasure in Rust

This is a very stripped-down version of a crypto library I am writing. There are cryptographic algorithms like RSA, DSA, ECDSA, EdDSA, ... which all have a similar pattern of having a private key, a ...
wigy's user avatar
  • 131
0 votes
1 answer
43 views

Swift append an array when it's been declared as Any

I'm learning Swift and I have a following problem: I create a dictionary containing some data, one of which is stored as an array. I want make an operation on this array to add some info. Currently I ...
gonczor's user avatar
  • 197
2 votes
0 answers
62 views

Creating an array of distinct objects with an identical method that can be evaluated (mapped) at compile-time (constexpr)

In a ray tracing project that I'm trying to make compile-time (constexpr) for fun and challenge, I've run into a bit of an issue: I have an object (intersection) that needs to refer to one of a group ...
Sebastian's user avatar
  • 143
0 votes
1 answer
897 views

create sorted vector of content of two binary search trees in sorted order

You are given two binary search trees, the goal is produce a sorted array of elements containing elements from both the trees. I wanted to know if there is a simpler approach, and if the use of ...
Kabira  K's user avatar
  • 111
3 votes
1 answer
500 views

Template Metaprogramming Discriminated Union

std::variant and boost::variant do not require the types involved to be unique, but lose some functionality when types repeat. ...
Greg Nisbet's user avatar
2 votes
1 answer
1k views

Variant implementation in C++

I've been fiddling with templates and compile time stuff i implemented a variant container and wanted some opinions and recommendations this is just a learning exercise, a couple template classes not ...
yer-boi's user avatar
  • 23
6 votes
0 answers
222 views

Value semantic type eraser

I've been using std::unique_ptr for cases where I want to store a derived-type value with type erasure, and felt that unique_ptr being nullable was an undesirable ...
Brent's user avatar
  • 451
11 votes
1 answer
2k views

Simple C++ alternative to exceptions for embedded systems

I have researched ways to implement a simpler alternative for C++ exceptions. The problem I had with exceptions was the complex and not-portable stack unwinding process which makes it hard to ...
Shmuel H.'s user avatar
  • 211
4 votes
0 answers
822 views

Custom basic std::variant

I was amazed by std::variant and got inspired to write my own. For now, it supports constructor+destructor pair, assignment operator and ...
Incomputable's user avatar
  • 9,374
2 votes
0 answers
616 views

C++ variant and variant subsets

I seem to be in a love-hate relationship with C++. It can make some parts of your code elegant and easy to grasp, and some others become an inconceivable mess. And it's not the language itself causing ...
Grieverheart's user avatar
15 votes
1 answer
434 views

An implementation of a static any type

This describes type util::any<std::size_t size, std::size_t align> - an implementation of std::any with a static size/no ...
user2296177's user avatar
  • 3,533
10 votes
1 answer
675 views

Variant class using generic programming

My goal is to learn a bit more about generic programming in C++. So, one of the trickiest things I've heard you can do is creating a Variant class. This took me a while and I needed to read and study ...
NeomerArcana's user avatar
2 votes
0 answers
85 views

Any class implementation - follow-up

Based on the previous question, I have improved it to make it insert regular function. Also, I have implemented a new template approach to obtain the signature from any function by testing its ...
MORTAL's user avatar
  • 3,278
6 votes
1 answer
3k views

Runtime compiler for getting/setting runtime property values

I've been playing about with runtime compilation to allow me to get/set runtime property values in C#, and so far I have come up with a class. This allows me to write code like the following which ...
James South's user avatar
3 votes
0 answers
270 views

Any class implementation

I was inspired from question here, to implement function container that holder any kind of function by using boost::any, I have carried on and extend it further to make the functions holder accept ...
MORTAL's user avatar
  • 3,278
3 votes
0 answers
200 views

Trivial full-fledged variant for constexpr (proof of concept)

I want to show you how to implement variant like boost::variant, but almost for constexpr, ...
Tomilov Anatoliy's user avatar
7 votes
3 answers
609 views

Type-agnostic BubbleSort in C

I'm trying out different sorting algorithms for learning purposes, here I'm doing a "type-agnostic" bubbleaort. I modeled the function signature after the standard library's ...
Zorgatone's user avatar
  • 436
2 votes
1 answer
417 views

Variant class (named any like in boost::any)

...
Mango's user avatar
  • 485
7 votes
1 answer
7k views

Storing collections of objects of any type

I've put together a class whose goal is to store objects of arbitrary type, with each type having its own vector for contiguity. I'd like to gather advice on what I have so far before I carry on. Let ...
Quentin's user avatar
  • 365
10 votes
1 answer
3k views

union based approach to implement variant

This implementation of discriminated union based on C++ inherent unions and inspired by this and this articles. ...
Tomilov Anatoliy's user avatar
4 votes
0 answers
179 views

any that can hold arrays

I've tried to make a better any than before: ...
user1095108's user avatar
  • 1,414
11 votes
2 answers
5k views

Multiple dispatch with variant and multi visitation in C++

The following program is a demonstration of variant and double visitation used to obtain double dispatch between the elements of two inhomogeneous containers. Most of the work is done at compile time. ...
DarioP's user avatar
  • 887
18 votes
2 answers
6k views

Variant class with full move support

I tried to write my own variant class, that is fully move-semantics enabled. WRT to implemented visitors, they don't require any policy and like to be derived from <...
Tomilov Anatoliy's user avatar
31 votes
1 answer
3k views

Yet another 'any' class implementation, named 'some'

This is a follow-up of 'any' class implementation. After posting my answer, I kept working on the code towards parameterizing with respect to memory management as I had promised. I ended up in some ...
iavr's user avatar
  • 2,274
11 votes
1 answer
1k views

Clone of Boost Variant

As part of learning C++, with special emphasis on C++11, I wanted to implement the equivalent of Boost's Variant (located here). My code is available at variant.hpp, with the current version given ...
ScootyPuff's user avatar
27 votes
2 answers
10k views

C++11 Any class

This is a polymorphic wrapper capable of holding any type. (It is loosely based on boost::any) In particular, this is useful when you want to store a ...
user1131146 account abandoned's user avatar
17 votes
2 answers
3k views

boost::any replacement with std::unique_ptr support

I needed a boost.any look-a-like, that could handle a std::unique_ptr. I came up with this. Please provide some C++11 criticism. ...
user1095108's user avatar
  • 1,414