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
4 votes
1 answer
153 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: ...
user avatar
  • 143
3 votes
1 answer
120 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 ...
user avatar
4 votes
1 answer
291 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 ...
user avatar
  • 196
4 votes
2 answers
223 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 ...
user avatar
  • 299
3 votes
1 answer
97 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. ...
user avatar
  • 303
6 votes
5 answers
353 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 ...
user avatar
  • 2,009
12 votes
5 answers
817 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, ...
user avatar
  • 2,050
11 votes
2 answers
243 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 ...
user avatar
  • 381
1 vote
0 answers
29 views

Dynamically build tagged union and helper that allows its instantiation and calling of its static methods

I am working on a typescript react/redux app that provides a visual interface for the code editor with a predefined set of instructions. It's basically a tree structure of objects that I call ...
user avatar
6 votes
1 answer
194 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 ...
user avatar
19 votes
2 answers
2k 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 ...
user avatar
3 votes
0 answers
668 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 ...
user avatar
  • 131
0 votes
1 answer
37 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 ...
user avatar
  • 197
2 votes
0 answers
54 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 ...
user avatar
  • 143
0 votes
1 answer
760 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 ...
user avatar
  • 111
3 votes
1 answer
417 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. ...
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 ...
user avatar
  • 23
6 votes
0 answers
213 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 ...
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 ...
user avatar
  • 211
4 votes
0 answers
739 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 ...
user avatar
  • 9,171
2 votes
0 answers
542 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 ...
user avatar
15 votes
1 answer
377 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 ...
user avatar
  • 3,473
10 votes
1 answer
622 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 ...
user avatar
2 votes
0 answers
82 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 ...
user avatar
  • 3,258
6 votes
1 answer
2k 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 ...
user avatar
3 votes
0 answers
260 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 ...
user avatar
  • 3,258
3 votes
0 answers
194 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, ...
user avatar
7 votes
3 answers
574 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 ...
user avatar
  • 436
2 votes
1 answer
410 views

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

...
user avatar
  • 485
7 votes
1 answer
6k 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 ...
user avatar
  • 365
10 votes
1 answer
2k views

union based approach to implement variant

This implementation of discriminated union based on C++ inherent unions and inspired by this and this articles. ...
user avatar
3 votes
0 answers
170 views

any that can hold arrays

I've tried to make a better any than before: ...
user avatar
  • 1,354
11 votes
2 answers
4k 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. ...
user avatar
  • 867
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 <...
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 ...
user avatar
  • 2,276
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 ...
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 ...
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. ...
user avatar
  • 1,354