Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2007-08-29 17:35:36


We have some class templates in forms like:

//==========================================================
template <unsigned X> struct my_test;

template <> struct my_test<0u> {};

template <unsigned X>
struct my_test { typedef my_test<(X - 1u)> type; };
//==========================================================

IOW, the traits express their result as a type, but omit the type for
out-of-domain values. I was looking at the enable_if documentation and
I was wondering if we can use SFINAE to create functions and/or function
templates that will test manifestations of "my_test," but skip the test
for out-of-domain values without have to explicitly exclude the values.
  Here, it's easy to omit "0" from the test file, but for some templates
it won't be so obvious which values to exclude. (Worse, the omitted
values may change between platforms!)

For example, could something like

//==========================================================
template < unsigned X >
typename my_test<X>::type print_out( my_test<X> const &x )
{
        std::cout << "There's a specialization at " << x
         << '.' << std::endl;
        return my_test<X>::type();
}

template < typename T >
void print_out( T const &x )
{ std::cout << "There's no 'type' here." << std::endl; }
//==========================================================

with some sort of compile-time list (maybe with MPL) that runs each
version of "my_test" through the best version of "print_out" be made?

(This is all for replacing the macros in "integer_test.cpp" with
something more up-to-date.)

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk