A wrapper around std::variant with some helper functions

Variant

A wrapper around std::variant with some helper functions

; std::vector container; container.emplace_back(First()); container.emplace_back(First()); container.emplace_back(Second()); //Visit polymorphic call: for (auto& element: container) element.visit([](auto& obj) { obj.process(); }); //Manual polymorphic call: for (auto& element: container) { if (auto* first = element.get ()) first->process(); else if (auto* second = element.get ()) second->process(); } return 0; } ">
#include <ea_variant/ea_variant.h>
#include <iostream>
#include <vector>

struct First
{
    void process() const
    {
        std::cout << "I am first! My value is " << myValue << std::endl;
    }

    int myValue = 5;
};

struct Second
{
    void process() const
    {
        std::cout << "I am second! My value is " << text << std::endl;
    }

    std::string text = "Hello World";
};

int main()
{
    using Var = EA::Variant
      ;
    std::vector
       container;

    container.emplace_back(First());
    container.emplace_back(First());
    container.emplace_back(Second());

    //Visit polymorphic call:
    for (auto& element: container)
        element.visit([](auto& obj) { obj.process(); });

    //Manual polymorphic call:
    for (auto& element: container)
    {
        if (auto* first = element.get
       
        ())
            first->
        process();
        
        else 
        if (
        auto* second = element.
        get
        
         ())
            second->
         process();
    }

    
         return 
         0;
}
        
       
     

I created this for a few reasons:

  1. std::visit has some issues in some compilers.
  2. A faster alternative, like rollbear::visit doesn't compile on MacOS
  3. I wanted to wrap this in a JUCE-style module
  4. I thought the syntax could be slightly simplified by wrapping in an object

How to use:

Include the ea_variant.h file. you can also use CMake to set up the include directories automatically

Visitation is using a modified version of: https://github.com/rollbear/visit

Owner
Eyal Amir
Musician turned into an instruments/plugin developer. Mostly working in C++/JUCE. Previous work including plugins I made for Waves, Xfer, Polyverse and deadmau5
Eyal Amir
Similar Resources

Recursive Variant: A simple library for Recursive Variant Types

Recursive Variant: A simple library for Recursive Variant Types

rva::variant — Recursive Sum Types for C++ Provided by the Recursive Variant Authority. We stand united in opposition to the TVA. May your variants ne

Nov 24, 2022

Libft is an individual project at 42 that requires us to re-create some standard C library functions including some additional ones that can be used later to build a library of useful functions for the rest of the program.

🔖 Index What is Libft? List of Functions Technologies ✨ What is Libft? Libft is an individual project at 42 that requires us to re-create some standa

Jan 17, 2022

Libft is an individual project at 42 that requires us to re-create some standard C library functions including some additional ones that can be used later to build a library of useful functions for the rest of the program.

Libft is an individual project at 42 that requires us to re-create some standard C library functions including some additional ones that can be used later to build a library of useful functions for the rest of the program.

Libft is an individual project at 42 that requires us to re-create some standard C library functions including some additional ones that can be used later to build a library of useful functions for the rest of the program.

Apr 5, 2022

high performance C++20 implementation of std::variant

A minimal compile-time overhead, C++20 implementation of std::variant. Fully standard conforming with a couple of documented differences.

Nov 22, 2022

A cleaner and more intuitive std::variant alternative

[WIP] ExtendedVariant This single header library is part of my C++ extended standard stdex libraries. Check our my profile for more. Working with C++

Jun 13, 2021

Finite State Machine implementation using std::variant

mp::fsm Implementation of Finite State Machine presented by me on CppCon 2018 in a talk Effective replacement of dynamic polymorphism with std::varian

Oct 10, 2022

An open source standard C library that includes useful functions && (Reimplementation of libc functions + own functions).

An open source standard C library that includes useful functions && (Reimplementation of libc functions + own functions).

📖 LIBFT-42 : Artistic view of LIBC: 🎭 HOW DOES IT FEEL HAVING YOUR OWN LIB: SUBJECT : ENGLISH PDF ℹ️ What is LIBFT : This project aims to code a C l

Nov 4, 2022

42 Cursus - Libft: My implementation of some useful C functions and some additional ones to use it in future projects of 42.

42 Cursus - libft Info My implementation of some useful C functions and some additional ones to use it in future projects of 42. Status: still updatin

Jul 21, 2022

Fast C++ container combining the best features of std::vector and std::deque

veque The double-ended vector A very fast C++17 container combining the best features of std::vector and std::deque "In Most Cases, Prefer Using deque

Nov 11, 2022

Invoke.hpp - std::invoke/std::apply analogs for C++11/14

invoke.hpp std::invoke/std::apply analogs for C++11/14 Requirements gcc = 4.9 clang = 3.8 msvc = 2015 Installation invoke.hpp is a header-only libr

Jun 30, 2022

Cpp-std-fwd - forward declarations for C++ std headers

cpp-std-fwd Forward declarations for most useful runtime classes of the C++ 17 standard library. DISCLAIMER: This project is meant as a proof-of-conce

Jul 10, 2022

A repository that includes common helper functions for writing applications in the DPDK. I will be using this for my future projects in the DPDK.

The DPDK Common (WIP) Description This project includes helpful functions and global variables for developing applications using the DPDK. I am using

Oct 30, 2022

A play around of mathematical functions to draw interesting objects to the screen.

A play around of mathematical functions to draw interesting objects to the screen.

LibDragonN64 Color Graphics Test A play around of mathematical functions to draw interesting objects to the screen. Compile Script (Windows only) In V

Dec 11, 2021

CMakeLists wrapper around imgui

ImGui Wrappings This is a trifold wrapper for the Dear ImGui library. Ease integration with CMake, Provide an RAII mechanism for ImGui scopes, Provide

Nov 3, 2022

curl4cpp - single header cURL wrapper for C++ around libcURL.

curl4cpp - single header cURL wrapper for C++ around libcURL.

Oct 13, 2022

lightweight, compile-time and rust-like wrapper around the primitive numerical c++ data types

prim_wrapper header-only, fast, compile-time, rust-like wrapper around the primitive numerical c++ data types dependencies gcem - provides math functi

Oct 22, 2021

sdl4cpp, header-only C++ wrapper around SDL2.

sdl4cpp sdl4cpp, header-only C++ wrapper around SDL2 (heavily work-in-progress) An example: #include "sdl4cpp/sdl4.hpp" #include "sdl4cpp/timer.hpp" #

Nov 28, 2021

Header only wrapper around Hex-Rays API in C++20.

HexSuite HexSuite is a header only wrapper around Hex-Rays API in C++20 designed to simplify the use of Hex-Rays and IDA APIs by modern C++ features.

Nov 12, 2022

Cython wrapper around C++ iostream

ciostream Provides a Cython wrapper around C++ iostream, allowing Python extensions to use Python streams with C++ code that uses C++ iostreams. Examp

Dec 16, 2021
Related tags
A C library for parsing/normalizing street addresses around the world. Powered by statistical NLP and open geo data.
A C library for parsing/normalizing street addresses around the world. Powered by statistical NLP and open geo data.

libpostal: international street address NLP libpostal is a C library for parsing/normalizing street addresses around the world using statistical NLP a

Nov 23, 2022
This is another way to bypass the restriction of calling dl functions for system lib.

简介 在Android7.0以及以上的版本中,dlfcn.h头文件中dlopen, dlsym函数已经无法在系统库上使用。 比较常见的规避方法是,先通过maps文件找到so文件对应起始地址,然后通过解析elf 文件,得到函数的偏移量,起始地址加上偏移量就算出函数的真实地址。 具体实现方式是:Noug

Nov 11, 2022
Quick check of NT kernel exported&unexported functions/global variable offset
 Quick check of NT kernel exported&unexported functions/global variable offset

NT内核导出以及未导出函数-全局变量偏移速查 Quick check of NT kernel exported&unexported functions/global variable offset System目录下有已经完成的偏移 可以在线速查 There are already comple

Oct 11, 2022
This project aims to code a C library regrouping usual functions.

42-formation-lvl1-1.libft Project 1 - Libft - First project for the formation of software engineers at school 42 São Paulo This project aims to code a

Jun 20, 2022
LXC Manager provide a set of functions to visually manage LXC unprivileged containers.

LXC Manager provide a set of functions to visually manage LXC unprivileged containers. The applciation use LXC Api to manage LXC. To use the application you must have LXC installed on your linux machine.

May 10, 2022
C implementation of C++ Utility functions Integer Comparison Macros

C implementation of C++ Utility functions Integer Comparison Macros

Oct 31, 2022
The Ultimate Raylib gaming library wrapper for Nim
The Ultimate Raylib gaming library wrapper for Nim

NimraylibNow! - The Ultimate Raylib wrapper for Nim The most idiomatic and up-to-date wrapper for Raylib gaming C library. Use this library if you wan

Nov 19, 2022
Header-only C++20 wrapper for MPI 4.0.

MPI Modern C++20 message passing interface wrapper. Examples Initialization: mpi::environment environment; const auto& communicator = mpi::world_c

Apr 8, 2022
Variant - C++17 `std::variant` for C++11/14/17

MPark.Variant C++17 std::variant for C++11/14/17 Introduction MPark.Variant is an implementation of C++17 std::variant for C++11/14/17. Based on my im

Nov 18, 2022
variant lite - A C++17-like variant, a type-safe union for C++98, C++11 and later in a single-file header-only library

variant lite: A single-file header-only version of a C++17-like variant, a type-safe union for C++98, C++11 and later Contents Example usage In a nuts

Oct 16, 2022