boost-variant

boost::variant recursive trouble

妖精的绣舞 提交于 2019-12-09 18:27:49
问题 is there any way to make this work? I hope you'll get the idea, I'm trying to create a list by means of recursive pairs #include <boost/variant.hpp> #include <utility> struct nil {}; typedef boost::make_recursive_variant<nil, std::pair<int, boost::recursive_variant_ >>::type list_t; int main() { list_t list = { 1, (list_t){ 2, (list_t){ 3, nil() } } }; return 0; } 回答1: No. The point of a boost::variant is that it has a fixed size, and does not do dynamic allocation. In this way it's similar

Construct a boost variant containing a value of the nth-type in the variant type index?

你说的曾经没有我的故事 提交于 2019-12-09 15:39:49
问题 I want to construct boost::variant s containing default-constructed values, specified with a type index - without writing my own switch statement over the type index. I figure this must be possible, somehow, with MPL? To clarify though, the index is not a compile-time constant expression. The use case is that I need to construct a variant which will later be replaced with one containing the correct value, but at this point I only know the type index. Think of it as a lazy deserialisation

How to check if a template type is one of the types of a variant type?

本秂侑毒 提交于 2019-12-08 19:31:50
问题 Considering a variant type and a template function, how can I check the template type is one of the types of the variant ? Is there a more elegant way than the following ? typedef boost::variant<Foo,Bar> Var; template <typename T> void f(const T& x) { BOOST_STATIC_ASSERT( boost::is_same<T,Foo>::value || boost::is_same<T,Bar>::value ); } Note : I use Boost 1.57 and gcc 4.8.3. I don't use C++11 for compatibility with old gcc versions. 回答1: Use MPL: #include <boost/variant/variant.hpp> #include

What is the right c++ variant syntax for calling a member function set to a particular variant?

本秂侑毒 提交于 2019-12-08 08:49:23
问题 The code below uses a boost variant of an std::map which contains int/MyVariant pairs. I am able to initialize my map correctly where the first element contains the 33/A pair and the second contains 44/B pair. A and B each have a function that I would like to be able to call after retrieving respectively their initialized map element: #include "stdafx.h" #include "boost/variant/variant.hpp" #include "boost/variant/get.hpp" #include "boost/variant/apply_visitor.hpp" #include <map> struct A {

How to convert a Boost::make_recursive_variant Object into a string?

自作多情 提交于 2019-12-08 04:36:14
问题 Been playing with Boost:make_recursive_variant and I'm quite stump on how to create a string from a given Variant and return it. I can output easily using cout, but my goal was to create a C++ Version of Arrays.deeptoString from java to return a string. Kept running into compile issues trying to work around recursive_variant. Here my Current Code for Arrays.deeptoString. typedef boost::make_recursive_variant<string, int, vector<boost::recursive_variant_ > >::type ObjectE; class deepToString :

boost variant copy semantics

为君一笑 提交于 2019-12-07 12:12:29
问题 I was wondering what the copy semantics of boost variants are. I've checked the source code and it's a bit baffling to me so I was wondering, in the example code, if my getVal(name) function makes a copy of the underlying vector when it's returned? If so, should I change it to be a reference (&) returned instead? using Val = boost::variant<std::vector<int>, std::vector<std::string>>; Val getVal(std::string& name) { return map[name];// where map is std::map<std::string, Val> } 回答1: Yes, your

Iterator for boost::variant

泄露秘密 提交于 2019-12-07 10:10:30
问题 Hy there, I'm trying to adapt an existing code to boost::variant. The idea is to use boost::variant for a heterogeneous vector. The problem is that the rest of the code use iterators to access the elements of the vector. Is there a way to use the boost::variant with iterators? I've tried typedef boost::variant<Foo, Bar> Variant; std::vector<Variant> bag; std::vector<Variant>::iterator it; for(it= bag.begin(); it != bag.end(); ++it){ cout<<(*it)<<endl; } But it didn't work. EDIT: Thank you for

How to implement a boost::variant derived-class?

喜夏-厌秋 提交于 2019-12-07 00:28:30
问题 I have tried for hours to code a class deriving from boost::variant . But I do not understand what is the problem (I do not understand what the compilation error means). What are the rules to implement a clean boost::variant derived-class? #include <boost/variant.hpp> class MyVariant : public boost::variant<char,bool> { public: MyVariant () : boost::variant<char,bool>( ) {} template <typename T> MyVariant( T& v) : boost::variant<char,bool>(v) {} template <typename T> MyVariant(const T& v) :

Reading a boost::variant type from istream

跟風遠走 提交于 2019-12-06 07:03:22
问题 I was going through boost::variant and wondering how can I make following to work ? typedef boost::variant<int,std::string> myval; int main() { std::vector<myval> vec; std::ifstream fin("temp.txt"); //How following can be achieved ? std::copy(std::istream_iterator<myval>(fin), //Can this be from std::cin too ? std::istream_iterator<myval>(), std::back_inserter(vec)); } For classes data members we have option to overload >> operator, but how to do this with myval ? 回答1: You can overload

C++: Nested map

依然范特西╮ 提交于 2019-12-06 06:31:41
问题 Here is the definition: struct nmap; struct nmap: map<string, boost::variant<string, nmap*>>{}; The last line below doesn't work: nmap my_map; my_map["a"] = "b"; my_map["c"] = new nmap; my_map["c"]["d"] = "e"; What do I need to add, in order for this to work? 回答1: I'd suggest either going for a tiny little readable helper: #include <boost/variant.hpp> #include <map> using std::map; struct nmap; struct nmap: map<std::string, boost::variant<std::string, nmap*>> { typedef boost::variant<std: