Boost logo

Boost Users :

From: John Moeller (fishcorn_at_[hidden])
Date: 2008-06-16 16:14:25


Hello all,

I am trying to make an "is_range" metafunction that (essentially) returns
mpl::true_ when the template parameter is a range (as defined by the
boost::range library).

I can do this if I create a specialization for every "non-range" type currently
specialized by the library (such as built-in arrays, char strings, etc.).
However, I'd like to have is_range work for a user-defined range without the
user *also* needing to specialize is_range.

I tried something like this:

//////////////////////////////////////////////////////////////////////
BOOST_MPL_HAS_XXX_TRAIT_DEF(type)

template < bool B >
struct is_range_impl
  : boost::mpl::false_
{};

template <>
struct is_range_impl< true >
  : boost::mpl::true_
{};

template < typename R >
struct is_range
  : is_range_impl< has_type< boost::range_iterator< R > >::value >
{};
//////////////////////////////////////////////////////////////////////

Which does fine if R is a range (even when you use the non-intrinsic method of
defining a custom class as a range), but if R is *not* a range, the compiler
complains that "iterator" is not a member of the class that I'm passing as the
parameter, because it defaults to using the embedded "iterator" type in the
parameter.

Is there a better way to define is_range so that it will work? I imagine that
I'd have to use SFINAE somehow, but I can't quite see how to put it together.

Many thanks ahead of time,

--
John Moeller

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