I have an existing codebase that makes fairly good use of Boost. In particular, it uses different flavours of boost::variant
in a few places:
typedef boost::variant<double, int, unsigned int, size_t, bool> MBVariant;
typedef boost::variant<double, float> FPVariant;
// ...
It also uses other Boost facilities like:
typedef boost::shared_ptr<int> MyPtr;
I am porting this code to a new platform that does not support Boost, but must be maintained alongside a platform that does. I am therefore creating a "Boost-wrapping" layer that will help remove the Boost dependency:
#ifdef USE_BOOST
# include <boost/shared_ptr.hpp>
# include <boost/variant.hpp>
template <class T>
using my_shared_ptr = boost::shared_ptr<T>
// how to wrap variant?
// template <typename T0_, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename T)>
// using my_variant = boost::variant<T0_, ???>;
#else
# include <memory>
// in the absence of Boost, use something else like std::shared_ptr:
template <class T>
using my_shared_ptr = std::shared_ptr<T>
// wrap variant with something non-Boost
template <typename T0, /* uh... */>
class variant { /* ... */ };
#endif
Now the client code can use my_shared_ptr or my_variant based on whether USE_BOOST is defined for the target plaform, or not:
typedef my_variant<double, int, unsigned int, size_t, bool> MBVariant;
typedef my_variant<double, float> FPVariant;
typedef my_shared_ptr<int> MyPtr;
The problem for me is that the boost::variant
is a class template that takes an arbitrary number of parameter types in the form of a recursive type list. I'm not sure if (or how) typedefs or type aliases can cope with this.
Is there a general way to wrap boost::variant
for this purpose?
I know that I could use simple typedefs to provide new type names for specific sets of parameters to boost::variant
, and I don't have a huge number at the moment, so that's a possibility, but I'm very interested in the general approach to type aliasing something like boost::variant
, while avoiding use of the preprocessor as much as possible as I need to integrate this into a namespace hierarchy.