是否可以在模板特化中匹配模板化基础?

Edw*_*nge 5 c++ templates metaprogramming

我当然可以使用is_base如果基类不是模板.但是,当它是,我只是没有看到任何方式来一般匹配任何派生类型.这是我的意思的基本例子:

#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();
}
Run Code Online (Sandbox Code Playgroud)

我想要那个回归true_而不是false_.真实示例有7个模板参数,大多数默认,并使用Boost.Parameter按名称引用它们.为了使用is_base我必须能够以某种方式拉出参数,我没有办法做到这一点,没有声明内部typedef.

我认为这是不可能的.希望被证明是错误的.

Max*_*kin 3

你只需要稍微调整你的测试:

#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;
}
Run Code Online (Sandbox Code Playgroud)