Re: function overload resolution by polymorphic types
In article
<e2cb44b9-59ec-43d0-910e-32b8148599cf@m36g2000hse.googlegroups.com>,
<"Roman.Perepelitsa@gmail.com"> wrote:
On 30 May, 20:27, "Hicham Mouline" <hic...@mouline.org> wrote:
I mean, if d1 is a type,
d2 is a template, and i know the n actual types generated from d2
(that i would expose to end users)
instead of writing
variant< d1, d2<typearg1>, d2<typearg2>, ..., d2<typeargn>, ... , d3>
i would write that with Prepocessor library somehow.
You can do that, but it will not be much less typing. It will look
like this:
#define MY_VARIANT_TYPES (typearg1)(typearg2)...(typeargn)
variant<d1, MY_TEMPLATE_INSTANCES(d2, MY_VARIANT_TYPES), d3>
Where MY_TEMPLATE_INSTANCES(x, (a)(b)..(z)) expands to
x<a>, x<b>,.., x<z>.
If you gonna use MY_TEMPLATE_INSTANCES macro more than once
then it makes sense to use preprocessor.
then, the static visitor could have a templated operator() member:
struct operation : boost::static_visitor<int> {
int operator()(const derived1&) const {
return 1;
}
template <typename T>
int operator()(const derived2<T>&) const {
// T-dependent code
return 2;
}
int operator()(const derived3&) const {
return 3;
}
};
right?
Yes, that's right.
Roman Perepelitsa.
or use mpl to generate a completed typelist
typedef typename boost::make_variant_over
<
boost::mpl::fold
<
boost::mpl::vector
<
type1,
type2,
type3,
// ...
typen
>,
d1,
boost::mpl::push_back<_2,d2<_1> >
>::type
::type variant_type;
If the list of types is used more than here they can be named
separately.
Beware boost::variant has a max of 20 types without tweaking.
Also the first type in the list [d1 above] must be default
constructable.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]