Is it possible to match the template base in specialized templates?

I could, of course, use is_baseit if the base class is not a template. However, when this is the case, I simply don’t see a way to generally match any derived type. Here is a basic example of what I mean:

#include <boost/mpl/bool.hpp>

template < typename T >
struct test_base
{
};

template < typename T >
struct check : boost::mpl::false_ {};

template < typename T >
struct check<test_base<T> > : boost::mpl::true_ {};

struct test_derived : test_base<int> {};

#include <iostream>
int main() 
{
  std::cout << check<test_derived>::value << std::endl;
  std::cin.get();
}

I want to return true_instead false_. This example has 7 template parameters, most defaults and uses Boost.Parameter to refer to them by name. To use is_base, I would have to somehow pull out the parameters, and I see no way to do this, not counting the internal typedefs.

I think it's impossible. Looking to be proven wrong.

+5
2

:

#include <iostream>
#include <boost/mpl/bool.hpp>

template < typename T >
struct test_base
{
};

template < typename T >
struct check_
{
    template<class U>
    static char(&do_test(test_base<U>*))[2];
    static char(&do_test(...))[1];
    enum { value = 2 == sizeof do_test(static_cast<T*>(0)) };
};

template < typename T >
struct check : boost::mpl::bool_<check_<T>::value> {};

struct test_derived : test_base<int> {};

int main()
{
  std::cout << check<test_derived>::value << std::endl;
}
+3

. is_base_and_derived is_same, .

#include "boost/mpl/equal.hpp"
#include "boost/mpl/vector.hpp"
#include <boost/utility/enable_if.hpp>
#include "boost/type_traits/is_base_and_derived.hpp"
#include <boost/function_types/function_type.hpp>

using namespace boost;

template < typename T >
struct test_base
{
};

struct test_derived : test_base<int> {};


//The default case
template<class T, class Enable =void>
class check : public boost::mpl::false_ {};

//The specified case
template<class T>
class check<T, typename boost::enable_if<
         boost::is_base_and_derived<test_base<int>,T>
    >::type>: public boost::mpl::true_  
{};


#include <iostream>
int main() 
{
    std::cout << check<test_derived>::value << std::endl;
    std::cin.get();
}
0

All Articles