Git Product home page Git Product logo

strict-variant's Introduction

strict variant

Build Status Appveyor status Boost licensed

Do you use boost::variant or one of the many open-source C++11 implementations of a "tagged union" or variant type in your C++ projects?

boost::variant is a great library. I created strict_variant in order to address a few things about boost::variant that I didn't like.

The tl;dr version is that unlike boost::variant or std::variant, strict_variant will never throw an exception or make a dynamic allocation in the effort of supporting types that have throwing moves. The default version will simply fail a static assert if this would happen. The strict_variant::easy_variant will make allocations in this situation, so you can opt-in to that if you want, and these two versions of variant "play nicely" together. This kind of thing is often a major concern in projects with realtime requirements, or in embedded devices, which may not allow, or simply may not have these C++ features. If you are making a library that might be used in "conventional" projects that want the ease-of-use that comes from boost::variant, but might also be used in projects with restrictive requirements, and you want to use a variant type as part of the API, strict_variant might offer a way to keep everyone happy.

Besides this, there are some issues in the interface of variant that were addressed that make it more pleasant to use day-to-day IMHO. (These were actually the original motivation of the project.)

  • I didn't like that code like this may compile without any warning or error messages:

    boost::variant<std::string, int> v;  
    
    v = true;  

    I'd usually rather that my variant is more restrictive about what implicit conversions can happen.

  • I wanted that things like this should compile and do what makes sense, even if overload resolution would be ambiguous.

    variant<bool, long, double, std::string> v;  
    
    v = true;  // selects bool
    v = 10;    // selects long 
    v = 20.5f; // selects double
    v = "foo"; // selects string

    I also wanted that such behavior (what gets selected in such cases) is portable.

    (For code examples like this, where boost::variant has unfortunate behavior, see "Abstract and Motivation" in the documentation.)

    In strict_variant we modify overload resolution in these situations by removing some candidates.

    For instance:

    • We eliminate many classes of problematic conversions, including narrowing and pointer conversions.
    • We prohibit standard conversions between bool, integral, floating point, pointer, character, and some other classes.
    • We impose "rank-based priority". If two integer promotions are permitted but one of them is larger than another, the larger one gets discarded,
      e.g. if int -> long and int -> long long are candidates, the long long is eliminated.

    See documentation for details.

  • I didn't like that boost::variant will silently make backup copies of my objects. For instance, consider this simple program, in which A and B have been defined to log all ctor and dtor calls.

    int main() {
      using var_t = boost::variant<A, B>;
    
      var_t v{A()};
      std::cout << "1" << std::endl;
      v = B();
      std::cout << "2" << std::endl;
      v = A();
      std::cout << "3" << std::endl;
    }

    The boost::variant produces the following output:

    A()
    A(A&&)
    ~A()
    1
    B()
    B(B&&)
    A(const A &)
    ~A()
    B(const B &)
    ~A()
    ~B()
    ~B()
    2
    A()
    A(A&&)
    B(const B &)
    ~B()
    A(const A &)
    ~B()
    ~A()
    ~A()
    3
    ~A()

    This may be pretty surprising to some programmers.

    By contrast, if you use the C++17 std::variant, or one of the variants with "sometimes-empty" semantics, you get something like this (this output from std::experimental::variant)

    A()
    A(A&&)
    ~A()
    1
    B()
    ~A()
    B(B&&)
    ~B()
    2
    A()
    ~B()
    A(A&&)
    ~A()
    3
    ~A()

    This is much closer to what the naive programmer expects who doesn't know about internal details of boost::variant -- the only copies of his objects that exist are what he can see in his source code.

    This kind of thing usually doesn't matter, but sometimes if for instance you are debugging a nasty memory corruption problem (perhaps there's bad code in one of the objects contained in the variant), then these extra objects, moves, and copies, may make things incidentally more complicated.

    Here's what you get with strict_variant:

    A()
    A(A&&)
    ~A()
    1
    B()
    B(B&&)
    ~A()
    ~B()
    2
    A()
    A(A&&)
    ~B()
    ~A()
    3
    ~A()

    Yet, strict_variant does not have an empty state, and is fully exception-safe!

    (These examples from gcc 5.4, see code in example folder.)

    To summarize the differences:

    • std::variant is rarely-empty, always stack-based. In fact, it's empty exactly when an exception is thrown. Later, it throws different exceptions if you try to visit when it is empty.
    • boost::variant is never-empty, usually stack-based. It has to make a dynamic allocation and a backup copy whenever an exception could be thrown, but that gets freed right after if an exception is not actually thrown.
    • strict_variant is never-empty, and stack-based exactly when the current value-type is nothrow moveable. It never makes a backup move or copy, and never throws an exception.

    Each approach has its merits. I chose the strict_variant approach because I find it simpler and it avoids what I consider to be drawbacks of boost::variant and std::variant. And, if you manage to make all your types no-throw move constructible, which I often find I can, then strict_variant gives you optimal performance, the same as std::variant, without an empty state.

For an in-depth discussion of the design, check out the documentation.

For a gentle intro to variants, and an overview of strict-variant, see slides from a talk I gave about this: [pptx][pdf]

Documentation

On github pages.

Compiler Compatibility

strict_variant targets the C++11 standard.

It is known to work with gcc >= 4.8 and clang >= 3.5, and is tested against MSVC 2015.

strict_variant can be used as-is in projects which require -fno-exceptions and -fno-rtti.

Licensing and Distribution

strict variant is available under the boost software license.

strict-variant's People

Contributors

brevzin avatar cbeck88 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

strict-variant's Issues

ambiguity issue with GCC

Thank you for providing your implementation of variant. I'm just playing around with it and stumbled over an issue. The following code builds with Clang but fails with GCC (5, 6, 7) due to an ambiguity of initializer<T>::operator():

struct S {
    explicit S (const std::string &str) {}
};

strict_variant::variant<std::string, S> var{std::string("string")};
variant.hpp:241:37: error: request for member ‘operator()’ is ambiguous
     return decltype(initializer<T>{}(std::declval<T>()))::value;

Is this intentional behavior or a bug? The code builds correctly with boost::variant.

Single-header version

Hi, thanks for this great project! I've been looking for something like this 🙂 std::variant IMO violates C++'s "don't pay for what you don't use" principle (e.g. lots of valueless_by_exception checks) and doesn't fit my projects.

Would it be possible to create a single-header version? I can concatenate the files myself, but it would be nice to have it automatically generated when there's a new release.

[EDIT] Just wanted to add that I get about 35% better overall performance in my benchmarks, compared to mpark::variant. This is great 👍

Combine the lambda of visitor

Hi,

Would you mine adding a make_visitor as seen on this page to allow using lambada with multiple type handling ?
Ref: https://bitbashing.io/std-visit.html

Ex :

[](auto& arg) {
    using T = std::decay_t<decltype(arg)>;

    if constexpr (std::is_same_v<T, string>) {
        printf("string: %s\n", arg.c_str());
        // ...
    }
    else if constexpr (std::is_same_v<T, int>) {
        printf("integer: %d\n", arg);
        // ...
    }
    else if constexpr (std::is_same_v<T, bool>) {
        printf("bool: %d\n", arg);
        // ...
    }
}

C4348 redefinition of default parameter (variant.hpp:183)

Hi! I'm getting this warning with MSVC 2015 (19.00.23918)

craftr.lib.strict-variant-1.0.0\data/strict-variant-0.3\include\strict_variant/variant.hpp(183): warning C4348: 'strict_variant::variant<int,std::string>::initializer': redefinition of default parameter: parameter 2
craftr.lib.strict-variant-1.0.0\data/strict-variant-0.3\include\strict_variant/variant.hpp(188): note: see declaration of 'strict_variant::variant<int,std::string>::initializer'
C:\Users\niklas\repos\craftr\craftr\examples\examples.cpp\main.cpp(113): note: see reference to class template instantiation 'strict_variant::variant<int,std::string>' being compiled

I'm not sure how to fix it, otherwise I'd make a PR 😊

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.