boost-variant

boost variant copy semantics

风流意气都作罢 提交于 2019-12-06 01:10:04
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> } Yes, your getVal returns a copy of the whole vectors (including copies of all the element strings, e.g.). Yes,

Iterator for boost::variant

六眼飞鱼酱① 提交于 2019-12-05 12:03:07
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 your help! But in my design, I need to get one element from the list and pass it around other parts of

C++ Mutually Recursive Variant Type (Again)

元气小坏坏 提交于 2019-12-05 06:03:51
I have a problem similar to that described here: C++ Mutually Recursive Variant Type I am trying to create a JSON representation in C++. Many libraries already offer excellent JSON representations and parsers that are very fast, but I am not reinventing this wheel. I need to create a C++ JSON representation that supports certain space optimizations under specific conditions. In short, if and only if a JSON array contains homogenous data, rather than storing every element as bloated variant types, I need compact storage of native types. I also need to support heterogeneous arrays and standard

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

佐手、 提交于 2019-12-05 04:16:51
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) : boost::variant<char,bool>(v) {} }; int main () { MyVariant a; MyVariant b = a; //compilation error //

boost variant type collision

被刻印的时光 ゝ 提交于 2019-12-05 03:22:20
问题 Follow-Up Question So, I've been playing with the Boost Mini C Tutorial What I have done is added a rule to parse string literals. The purpose is so that I can parse and compile programs like (functionality already built-in): int ret(int x) { return x; } int main() { int x = 5; return ret(x)*2; } As well as ( want to add this functionality), string print(string s) { return s; } int main() { string foo = "bar"; print(foo); return 0; } Whether or not the last two examples compile with say gcc,

Reading a boost::variant type from istream

十年热恋 提交于 2019-12-04 14:45:24
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 ? You can overload operator>> for variant just like for any other type. But it is up to you to implement the logic to decide what

How to use comparison operators on variant with contained types?

别等时光非礼了梦想. 提交于 2019-12-04 13:35:55
问题 I'm using variant a lot in my code and I need to make comparisons with the content in some places to test the content of the variant for its value. For example: if(equals<int>(aVariant, 0)){ //Something } else { //Something else } with this simple template function I've written for this purpose: template<typename V, typename T> inline bool equals(V& variant, T value){ return boost::get<T>(&variant) && boost::get<T>(variant) == value; } This works well, but the code starts to be difficult to

boost::variant recursive trouble

馋奶兔 提交于 2019-12-04 09:46:34
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; } 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 to a union. A recursive boost::variant would have to have infinite size in order to contain its largest

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

人盡茶涼 提交于 2019-12-04 02:42:12
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 problem. You need to use the variant::types typedef. This gives you an MPL compatible sequence which we can

boost variant type collision

痞子三分冷 提交于 2019-12-03 17:32:54
Follow-Up Question So, I've been playing with the Boost Mini C Tutorial What I have done is added a rule to parse string literals. The purpose is so that I can parse and compile programs like (functionality already built-in): int ret(int x) { return x; } int main() { int x = 5; return ret(x)*2; } As well as ( want to add this functionality), string print(string s) { return s; } int main() { string foo = "bar"; print(foo); return 0; } Whether or not the last two examples compile with say gcc, is inconsequential. So, the gist of what I added is the following: Within the file expression_def.hpp