Created
October 29, 2012 20:38
-
-
Save dabrahams/3976374 to your computer and use it in GitHub Desktop.
Variant size test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright Dave Abrahams 2012. Distributed under the Boost | |
// Software License, Version 1.0. (See accompanying | |
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
# include <vector> | |
# include <map> | |
# include <string> | |
# include <boost/variant.hpp> | |
namespace tst { | |
using namespace boost; | |
struct null{}; | |
namespace no_wrapper | |
{ | |
struct value; | |
typedef std::map<std::string, value> object; | |
typedef std::vector<value> array; | |
typedef variant< | |
null, std::string, double, object, array, bool | |
> value_base; | |
struct value : value_base | |
{ | |
value() {}; | |
template <class T> | |
value(T const& x) : value_base(x) {} // no move; I'm in C++03-land | |
}; | |
} | |
namespace wrapper | |
{ | |
struct value; | |
typedef std::map<std::string, value> object; | |
typedef std::vector<value> array; | |
typedef variant< | |
null, std::string, double, | |
recursive_wrapper<object>, | |
recursive_wrapper<array>, | |
bool | |
> value_base; | |
struct value : value_base | |
{ | |
value() {}; | |
template <class T> | |
value(T const& x) : value_base(x) {} // no move; I'm in C++03-land | |
value& operator=(value const& x) { | |
value_base const& x_ = x; | |
value_base::operator=(x_); | |
return *this; | |
} | |
}; | |
} | |
namespace easy { | |
typedef make_recursive_variant< | |
null, std::string, double, | |
std::map<std::string, recursive_variant_>, | |
std::vector<recursive_variant_>, | |
bool | |
>::type value; | |
typedef std::map<std::string, value> object; | |
typedef std::vector<value> array; | |
} | |
} | |
# include <iostream> | |
int main() | |
{ | |
tst::no_wrapper::array a1; | |
tst::wrapper::array a2; | |
tst::easy::array a3; | |
tst::no_wrapper::object o1; | |
tst::wrapper::object o2; | |
tst::easy::object o3; | |
tst::no_wrapper::value v1 = a1; | |
tst::wrapper::value v2 = a2; | |
tst::easy::value v3 = a3; | |
std::cout << "size of objects: " << sizeof(o1) << ", " << sizeof(o2) << ", " << sizeof(o3) << std::endl; | |
std::cout << "size of arrays: " << sizeof(a1) << ", " << sizeof(a2) << ", " << sizeof(a3) << std::endl; | |
std::cout << "size of variants: " << sizeof(v1) << ", " << sizeof(v2) << ", " << sizeof(v3) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment