Skip to main content

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
1 vote
0 answers
51 views

Simple CRUD server in rust

This is my first rust application. I tried to pick somewhat low level dependencies to get a feel for the language particulars. I'm especially interested in control flow/error patterns in rust as I ...
intentionally-left-nil's user avatar
2 votes
1 answer
77 views

Using std::variant

Have been using std::variant for the first time. Accidentally stumbled on a pattern that seems resonable but wanted to get a second opinion. I have a class that can ...
Loki Astari's user avatar
  • 95.4k
-2 votes
1 answer
87 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
181 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
201 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
610 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
546 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
440 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
839 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
2k 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
576 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
1k 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
567 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
362 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,029
13 votes
6 answers
969 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,395
11 votes
2 answers
266 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
292 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
23 votes
2 answers
5k 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
970 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
44 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
65 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
922 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
518 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
224 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
861 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,504
2 votes
0 answers
641 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
450 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,543
10 votes
1 answer
704 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
86 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
273 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
620 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
419 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
181 views

any that can hold arrays

I've tried to make a better any than before: ...
user1095108's user avatar
  • 1,434
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
  • 897
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,284
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,434