Variant type for C++
This is similar to Boost.Any in that any type can be encapsulated and retrieved, but this implementation does not have any dependencies which keeps the footprint much smaller than Boost.Any.
The any
variant type is distributed only as a header. There is nothing to compile in this
repository other than tests which also illustrate how to use any
.
- CMake 2.8.12 or newer
- C compiler. The following have been tested:
- llvm 3.5 or newer
- gcc 4.8.2 or newer
- Visual Studio 2013 Update 4
All tests have passed on OS X 10.10, Ubuntu 14.04.1, Ubuntu 14.10, and Fedora 21.
I highly recommend building outside of the source tree so that build products do not pollute the repository. The simplest way to accomplish this is to create a sub-directory named "build" as follows:
$ mkdir build
$ cd build
$ cmake ..
$ make
Construct from a machine type:
type::any a = 3.14159;
std::cout << "a = " << a.as<double>() << std::endl;
Construct from a constant array (e.g. C-style string):
type::any a = "string";
std::cout << "a = " << a.as<char const*>() << std::endl;
Construct from an object reference
type::any a = std::string("string");
std::cout << "a = " << a.as<std::string>() << std::endl;
Construct from an object pointer:
struct obj {
char const* str() { return "string"; }
};
obj* o = new obj();
type::any a = o;
std::cout << "a = " << a.as<obj*>()->str() << std::endl;
delete o;
- Add type check to type::any::as method.
- Add more tests.
- Fork it
- Create a feature branch (
git checkout -b new-feature
) - Commit changes (
git commit -am "Added new feature xyz"
) - Push the branch (
git push origin new-feature
) - Create a new pull request.
- Paul Howes (http://github.com/PaulHowes/)
type::any
variant type copyright 2015 Paul Howes and is licensed under the Apache License.