boost-any

boost::spirit::hold_any memory corruption

徘徊边缘 提交于 2019-12-09 01:18:43
问题 I have a large code base that can use boost::any or boost::spirit::hold_any (depending on a macro definition). hold_any seems to be compatible with boost::any (e.g. How to print boost::any to a stream? or Type erasure - Part IV) and faster (Why you shouldn’t use boost::any) but I'm experiencing several segmentation fault errors using hold_any (Boost v1.55 / 1.54 / 1.53). This is a minimal working example that exhibits the same problem as the original code: #include <iostream> #include <string

Different key type in a map

柔情痞子 提交于 2019-12-08 08:17:31
问题 for a particular requirement I want to have a map with keys in different type. Similar to boost:any. (I have an old gcc version) map<any_type,string> aMap; //in runtime : aMap[1] = "aaa"; aMap["myKey"] = "bbb"; is this something possible with the use boost ? thank in advance 回答1: If you're not willing to use boost variant, you can hack your own key type. You could use a discriminated union, or go low-tech en simply use a pair of std::string and int: Live On Coliru #include <map> #include

Accessing Values in a Class Similar to boost::any

断了今生、忘了曾经 提交于 2019-12-07 16:12:23
问题 I'm making a simple boost::any -like class for educational purposes, but I can't figure out how to access the stored value. I can set the value perfectly, but when I try to access any member in the "holder" class the compiler just complains that the member wasn't found in the class it was derived from. I can't declare the members as virtual because of the templates. Here's the relevant code: class Element { struct ValueStorageBase { }; template <typename Datatype> struct ValueStorage: public

c++ boost::any to define my own print ,

◇◆丶佛笑我妖孽 提交于 2019-12-03 11:19:41
问题 Am struggling a lot to find how to do to use boost::any to create a print function that can print any type using template first. template <typename T> struct printer { void print(ostream& os, const boost::any& a); }; I need to define first print() . i wish to have the real operator << for any, The idea is simple: attach to each any object an instance of class printer<T> with the suitable T and change this object when the value type of the any changes. A first technical problem is that the

c++ boost::any to define my own print ,

让人想犯罪 __ 提交于 2019-12-03 00:48:22
Am struggling a lot to find how to do to use boost::any to create a print function that can print any type using template first. template <typename T> struct printer { void print(ostream& os, const boost::any& a); }; I need to define first print() . i wish to have the real operator << for any, The idea is simple: attach to each any object an instance of class printer<T> with the suitable T and change this object when the value type of the any changes. A first technical problem is that the printer object depends on T whereas any is not (and should not be) a class template. Please I really need

Boost.Any vs. Boost.Variant

試著忘記壹切 提交于 2019-12-02 17:23:57
I'm having trouble choosing between Boost.Any and Boost.Variant. When should I use each one? What are the advantages and disadvantages of each? I am basically looking to store some states from external sources. drby Have you looked at the comparison in the variant library already? (Not sure what states from external sources are, so it's kind of hard to say what's more appropriate for you.) 来源: https://stackoverflow.com/questions/1366524/boost-any-vs-boost-variant

Why boost::any does not hold string literal?

我是研究僧i 提交于 2019-12-01 20:22:59
问题 #include <boost/any.hpp> #include <list> #include <string> #include <vector> struct _time_t { int month; int year; }; int main() { std::string str = "hahastr"; _time_t t; std::vector<boost::any> objVec; objVec.push_back(1); char* pstr = "haha"; //boost::any charArr = "haha"; not compile //objVec.push_back("haha"); not compile objVec.push_back(pstr); objVec.push_back(str); objVec.push_back(t); return 0; }; the commented code lines do not compile, why? I think string literal could act as char*

Why boost::any does not hold string literal?

我的梦境 提交于 2019-12-01 19:34:58
#include <boost/any.hpp> #include <list> #include <string> #include <vector> struct _time_t { int month; int year; }; int main() { std::string str = "hahastr"; _time_t t; std::vector<boost::any> objVec; objVec.push_back(1); char* pstr = "haha"; //boost::any charArr = "haha"; not compile //objVec.push_back("haha"); not compile objVec.push_back(pstr); objVec.push_back(str); objVec.push_back(t); return 0; }; the commented code lines do not compile, why? I think string literal could act as char* in most circumstance, is this related r-value and l-rvalue? error message: test_boost_any.cc D:\Program

C++: How to create a vector storing vectors of any type?

你说的曾经没有我的故事 提交于 2019-12-01 14:11:28
I'd like to store vectors of any type in another vector. So, for example I have two vector instances, "std::vector v1" and "std::vector v2". And I would like to put them into a vector. I already tried like this: std::vector<int> v1; std::vector<std::string> v2; std::vector< std::vector<boost::any> > vc; vc.push_back(v1); And several other ways, but nothing works. Do you know a possible solution? Thanks! Disclaimer: I don't recommend this. But if you insist, here is the beginnings of such an abomination: #include <vector> #include <memory> #include <functional> #include <boost/any.hpp> class

C++: How to create a vector storing vectors of any type?

主宰稳场 提交于 2019-12-01 12:51:53
问题 I'd like to store vectors of any type in another vector. So, for example I have two vector instances, "std::vector v1" and "std::vector v2". And I would like to put them into a vector. I already tried like this: std::vector<int> v1; std::vector<std::string> v2; std::vector< std::vector<boost::any> > vc; vc.push_back(v1); And several other ways, but nothing works. Do you know a possible solution? Thanks! 回答1: Disclaimer: I don't recommend this. But if you insist, here is the beginnings of such