Is it possible to match templated base in template specializations?
I could of course use is_base
if the base class where not a template. However, when it is, I just don't see any way to generically match any derived type. Here's 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_ 开发者_Python百科{};
struct test_derived : test_base<int> {};
#include <iostream>
int main()
{
std::cout << check<test_derived>::value << std::endl;
std::cin.get();
}
I want that to return true_
rather than false_
. The real example has like 7 template parameters, most defaulted, and uses Boost.Parameter to refer to them by name. In order to use is_base
I'd have to be able to pull the parameters out somehow and I don't see a way to do that short of declaring internal typedefs.
I think it's impossible. Looking to be proven wrong.
You just need to tweak your test a bit:
#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;
}
精彩评论