boost-variant

boost::variant; std::unique_ptr and copy

我是研究僧i 提交于 2019-11-30 03:54:16
问题 This Question Determined That a Non-Copyable Type Can't Be Used With Boost Variant Tree class template <class T = int> class Tree{ private: class TreeNode{ public: std::unique_ptr Nodes Move constructors and move assignment + other public members private: TreeNode(const TreeNode &other); (= delete not supported on compiler) TreeNode& operator=(const TreeNode &rhs); (= delete not supported on compiler) }; // End Tree Node Class Definition Tree(const Tree &other); (= delete not supported on

Generic function to convert boost::any to boost::variant

白昼怎懂夜的黑 提交于 2019-11-29 14:52:50
Assume that you have a boost::any object and a boost::variant object. I'm looking for a generic function convert , that takes a template parameter T being a specialized boost::variant e.g. boost::variant<int, std::string> and magically converts the boost::any to one of the available types of the given boost::variant . template<T> T convert(const boost::any& any) { // Some generic conversion code here or throw exception if conversion is not possible! } int main(int argc, char** args) { typedef boost::variant<int, std::string> TVar; boost::any any="Hello World"; TVar variant=convert<TVar>(any);

Is it safe to serialize a raw boost::variant?

最后都变了- 提交于 2019-11-28 11:27:55
boost::variant claims that it is a value type. Does this mean that it's safe to simply write out the raw representation of a boost::variant and load it back later, as long as it only contains POD types? Assume that it will be reloaded by code compiled by the same compiler, and same version of boost, on the same architecture. Also, (probably) equivalently, can boost::variant be used in shared memory? Regarding serialisation: It should work, yes. But why don't you use boost::variant 's visitation mechanism to write out the actual type contained in the variant? struct variant_serializer : boost:

Generic function to convert boost::any to boost::variant

て烟熏妆下的殇ゞ 提交于 2019-11-28 09:16:20
问题 Assume that you have a boost::any object and a boost::variant object. I'm looking for a generic function convert , that takes a template parameter T being a specialized boost::variant e.g. boost::variant<int, std::string> and magically converts the boost::any to one of the available types of the given boost::variant . template<T> T convert(const boost::any& any) { // Some generic conversion code here or throw exception if conversion is not possible! } int main(int argc, char** args) { typedef

What are the differences between std::variant and boost::variant?

大城市里の小女人 提交于 2019-11-28 00:45:01
In an answer to this SO question: What is the equivalent of boost::variant in the C++ standard library? it is mentioned that boost::variant and std::variant differ somewhat. What are the differences, as far as someone using these classes is concerned? What motivation did the committee express to adopt std::variant with these differences? What should I watch out for when coding with either of these, to maintain maximum compatibility with switching to the other one? (the motivation is using boost::variant in pre-C++17 code) Assignment/emplacement behavior: boost::variant may allocate memory when

boost::variant - why is “const char*” converted to “bool”?

我的梦境 提交于 2019-11-27 23:32:04
I have declared a boost::variant which accepts three types: string , bool and int . The following code is showing that my variant accepts const char* and converts it to bool . Is it a normal behavior for boost::variant to accept and convert types not on its list? #include <iostream> #include "boost/variant/variant.hpp" #include "boost/variant/apply_visitor.hpp" using namespace std; using namespace boost; typedef variant<string, bool, int> MyVariant; class TestVariant : public boost::static_visitor<> { public: void operator()(string &v) const { cout << "type: string -> " << v << endl; }

best way to do variant visitation with lambdas

点点圈 提交于 2019-11-27 18:10:39
I want to inline visitation of variant types with lambdas. At the moment i have the following code: struct Foo { boost::variant< boost::blank , int , string , vector< int > > var; template <typename T, typename IL , typename SL , typename VL> void ApplyOptionals( T& ref, IL&& intOption , SL&& stringOption , VL&& vectorOption ) { if (var.which() == 1) { intOption( ref , boost::get< int >(var) ); } else if (var.which() ==2) { stringOption( ref , boost::get< string>(var) ); } else if (var.which() == 3) { vectorOption( ref , boost::get< vector< int > >(var) ); } }; }; // ... myFooV.ApplyOptionals(

How do boost::variant and boost::any work?

∥☆過路亽.° 提交于 2019-11-27 17:06:16
How do variant and any from the boost library work internally? In a project I am working on, I currently use a tagged union. I want to use something else, because unions in C++ don't let you use objects with constructors, destructors or overloaded assignment operators. I queried the size of any and variant, and did some experiments with them. In my platform, variant takes the size of its longest possible type plus 8 bytes: I think it my just be 8 bytes o type information and the rest being the stored value. On the other hand, any just takes 8 bytes. Since i'm on a 64-bit platform, I guess any

best way to do variant visitation with lambdas

蹲街弑〆低调 提交于 2019-11-27 04:15:58
问题 I want to inline visitation of variant types with lambdas. At the moment i have the following code: struct Foo { boost::variant< boost::blank , int , string , vector< int > > var; template <typename T, typename IL , typename SL , typename VL> void ApplyOptionals( T& ref, IL&& intOption , SL&& stringOption , VL&& vectorOption ) { if (var.which() == 1) { intOption( ref , boost::get< int >(var) ); } else if (var.which() ==2) { stringOption( ref , boost::get< string>(var) ); } else if (var.which(

What are the differences between std::variant and boost::variant?

有些话、适合烂在心里 提交于 2019-11-26 21:46:47
问题 In an answer to this SO question: What is the equivalent of boost::variant in the C++ standard library? it is mentioned that boost::variant and std::variant differ somewhat. What are the differences, as far as someone using these classes is concerned? What motivation did the committee express to adopt std::variant with these differences? What should I watch out for when coding with either of these, to maintain maximum compatibility with switching to the other one? (the motivation is using