4 thoughts on “C++ Reflection Part 4: Variant

  1. Lee R

    Hi Randy,

    First off, I think you’ve created a great blog here, with some really useful articles. Keep it up :)

    However, I’d like to point out that most of the code presented in this post shouldn’t even compile (and doesn’t, in GCC 4.7.2). The META_TYPE macro accesses the dependant type name ‘type’ from the class ‘RemQual’.  This should result in a compilation error when META_TYPE is used inside a template class/function. The appropriate fix depends on the desired level of portability.


    1. Randy Gaul Post author

      Hey thanks for pointing that out! I do use MS’s compiler most of the time. If I find some extra time I will come back here and cleanup for GNU. It’s really just a matter of spare time :)

  2. Sean

    Hey Randy,

    I found your site while researching (happens more and more lately) the various C++ variant pre-proposals and ideas flying around right now. Just wanted to point out that what  you’re calling a variant is more like boost::any (which may be in C++17 or a Library TS) and not a boost::variant (which is not being proposed, but others are proposing similar libraries under similar names).

    The difference is that variant in the current C++ vernacular is a sum type, a type composed of multiple other types, as in it can only store a specific set of types. variant<int, float, string> for instance. This is advantageous when you might want a sum type including a non-copyable since then the whole variant becomes non-copyable. It’s also useful for compile-time pattern matching.

    Here’s the code samples from a CppCon talk whose authors are planning to propose variant in the near future: https://github.com/JasonL9000/cppcon14 (their slides still aren’t up, sadly).

    If you want to play around with sum types or learn more about them, Rust (or Haskell or many other functional languages) are a good place to look.

    Pointing this out so for any future articles or code you write on this topic you can be aware that variant might be a confusing name for this purpose in the near future. :)

    1. Randy Gaul Post author

      Thanks for the comment Sean! In my own code I’ve renamed this to “Variable”; Variable is much more an appropriate name!


Leave a Reply

Your email address will not be published. Required fields are marked *