Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Variant: how to get currently held type?

As I understood all types of boost.variant are parsed into real types (meaning as if boost variant<int, string> a; a="bla-bla" would after compilation turn into string a; a="bla-bla") And so I wonder: how to get what type was put into boost variant?

What have I tried:

#include <boost/variant.hpp> #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <iostream>  int main() {     typedef boost::function<double (double x)> func0;     typedef boost::function<double (double x, double y)> func1;     typedef boost::variant<int, func0, func1> variant_func;     func1 fn = std::plus<double>();     variant_func v(fn);     std::cout << boost::get<func1>(v)(1.0, 1.0) << std::endl; // this works     //std::cout << boost::get<v::type>(v)(1.0, 1.0) << std::endl; // this does not compile with many errors     // std::cout << (v)(1.0, 1.0) << std::endl; // this fails with Error    1   error C2064: term does not evaluate to a function taking 2 arguments      std::cin.get();     return 0; } 
like image 406
myWallJSON Avatar asked Dec 01 '11 15:12

myWallJSON


People also ask

How do I know which boost variant I have?

boost. variant has a . type() function which can return the typeid of the active type, provided you've enabled RTTI. You could also define a static visitor to perform actions depending on the type of content of the variant, e.g.

How does boost variant work?

boost::variant is conceptually similar to what you've done before, but by not literally using a union and instead taking a manual approach to placement construction/destruction of objects in its buffer (while handling alignment issues explicitly) it works around the restrictions that C++ has re complex types in actual ...

What is boost visitor?

boost::apply_visitor — Allows compile-time checked type-safe application of the given visitor to the content of the given variant, ensuring that all types are handled by the visitor.

What is boost any?

The boost::any class (based on the class of the same name described in "Valued Conversions" by Kevlin Henney, C++ Report 12(7), July/August 2000) is a variant value type based on the second category. It supports copying of any value type and safe checked extraction of that value strictly against its type.


1 Answers

v.which() will return the 0-based index of the type of the object currently held.

When you are retrieving the object your code must use a static type (in order to satisfy the get<T> function template) to refer to an (effectively) dynamically typed object.

You need to either test for the type (using which() or type()) and branch accordingly or use a static visitor. No matter which way you choose, you have to explicitly state the static type that you want to retrieve and it has to match the dynamic type or an exception will be thrown.

One way around this problem is instead of using a variant type directly, use a class which contains a variant type internally and then defines any implicit conversion operators necessary to use the object with minimum fuss.

I have a project called Dynamic C++ which uses this technique.

like image 200
Ferruccio Avatar answered Sep 19 '22 11:09

Ferruccio