Boost logo

Boost Users :

Subject: Re: [Boost-users] enable_if and multiple partial specializations
From: Gennadiy Rozental (rogeeff_at_[hidden])
Date: 2011-04-08 23:14:10


Stirling Westrup <swestrup <at> gmail.com> writes:

> This suggestion really is your best bet. To elaborate a bit further,
> you don't really even want to use enable_if in this case. You really
> want to write something like:
>
> #include <boost/mpl/if.hpp>
> #include <boost/type_traits/is_enum.hpp>
> #include <boost/type_traits/is_scalar.hpp>
>
> #include <iostream>
>
> struct tag_other{};
> struct tag_scalar{};
> struct tag_enum : tag_scalar{};
>
> // there are many better ways of writing tag_lookup.
> // This is just an example.
> template<typename T>
> struct tag_lookup {
> typedef typename boost::mpl::if_<
> boost::is_scalar<T>,
> typename boost::mpl::if_<
> boost::is_enum<T>, tag_enum, tag_scalar
> >::type, tag_other
> >::type type;
> };
>
> template<typename T> struct M_impl;
>
> template<>
> struct M_impl<tag_other> {
> static void foo() { std::cout << "generic" << std::endl; }
> };
>
> template<>
> struct M_impl<tag_scalar> {
> static void foo() { std::cout << "scalar" << std::endl; }
> };
>
> template<>
> struct M_impl<tag_enum> {
> static void foo() { std::cout << "enum" << std::endl; }
> };

This has at least 2 problems:

1. It does not work. I need "scalar" level specialization to be applied if enum
level specialization is not present. In your case it will not even compile. Or
it will choose incorrect (generic) option if you non specialized version for
that. We can try to follow STL's approach of iterator tag dispatching based on
hierarhy, but that really cumbersome and require extra runtime wrappers.

2. It's intrusive. Once I introduce new subgroup of types I need to go in common
header and update tag selection (and potentially something else to handle
relation with other groups)

That's said I did not come up with something better yet.

Gennadiy


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net