Git Product home page Git Product logo

cpp-enum-variant's Introduction

cpp-enum-variant

A simple variant data type similar to boost::variant, designed to be easy to use, intuitive, and not to be a big pile of macros.

Including

Using this library is as simple as including enum.hpp in your project. Include optional.hpp for a simple Optional<T> implementation using it, or tree.hpp for a proof-of-concept BST implementation.

It requires a decent C++14 compiler - I have tested it in VS2015, gcc, and clang.

Using

Create a type like so:

using Test = venum::Enum
  ::Variant<int>
  ::Variant<char>;

or the less verbose

using Test = venum::EnumT<int, char>;

The type is made using std::aligned_storage and a single int tag, and so is as big as the biggest variant + an int.

To construct an instance, simply call the constructor with whatever arguments. The variant you want should be inferred correctly from the arguments:

Test int_test(5) // produces an int variant
Test char_test('a') // produces a char variant

If you absolutely must specify the variant you want, use construct<T>:

auto test = Test::construct<int>('a') // produces an int variant

If, in any of these cases, a valid construtor can not be found, the program will fail to compile.

To get the data out of the object, you use match, which takes a function for each possible variant (in order) and applies the correct one:

std::string s = test.match(
  [](int i) { std::ostringstream str; str << "int: " << i; return str.str(); },
  [](char c) { std::ostringstream str; str << "char: " << c; return str.str(); }
);

You can also use apply, which takes a single polymorphic function:

test.apply([](auto& value) { std::cout << value << std::endl; });

And that's it!

cpp-enum-variant's People

Contributors

samuelsleight avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.