I have two structs:
// ----- non-const -----
struct arg_adapter
{
EArgType type; // fmtA, fmtB, ...
union
{
TypeA * valueA;
TypeB * valueB;
// ... more types
}
arg_adapter(TypeA & value) : type(fmtA), valueA(&value) {}
arg_adapter(TypeB & value) : type(fmtB), valueB(&value) {}
// ...
}
// ----- const version -----
struct const_arg_adapter
{
EArgType type; // fmtA, fmtB, ...
union
{
TypeA const * valueA;
TypeB const * valueB;
// ... more types
}
arg_adapter(TypeA const & value) : type(fmtA), valueA(&value) {}
arg_adapter(TypeB const & value) : type(fmtB), valueB(&value) {}
// ...
}
They are supposed to be used in methods such as:
Convert(const_arg_adapter from, arg_adapter to)
There are multiple TypeX' (about 5, may become more), most of them primitive. This is to avoid maintaining different prototypes.
Now my question ;-)
Is there a way to make the const-ness a template parameter? My goal is to maintain only one struct, i.e.
template <Qualifier CONSTNESS>
struct arg_adapter_t
{
...
CONSTNESS TypeA * valueA;
...
}
You can make it accept a metafunction and you can apply any transformation you like
template<template<typename> class F>
struct arg_adapter
{
EArgType type; // fmtA, fmtB, ...
union
{
typename F<TypeA>::type * valueA;
typename F<TypeB>::type * valueB;
// ... more types
};
arg_adapter(typename F<TypeA>::type & value) : type(fmtA), valueA(&value) {}
arg_adapter(typename F<TypeB>::type & value) : type(fmtB), valueB(&value) {}
// ...
};
typename arg_adapter<boost::add_const> const_adapter;
typename arg_adapter<boost::mpl::identity> nonconst_adapter;
Or accept a metafunction class to get more flexibility (including the ability to make F
have default arguments not known to your arg_adapter
and such.
template<typename F>
struct arg_adapter
{
EArgType type; // fmtA, fmtB, ...
union
{
typename apply<F, TypeA>::type * valueA;
typename apply<F, TypeB>::type * valueB;
// ... more types
};
arg_adapter(typename apply<F, TypeA>::type & value) : type(fmtA), valueA(&value) {}
arg_adapter(typename apply<F, TypeB>::type & value) : type(fmtB), valueB(&value) {}
// ...
};
typename arg_adapter< lambda< boost::add_const<_> >::type > const_adapter;
typename arg_adapter< lambda< boost::mpl::identity<_> >::type > nonconst_adapter;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With