代码之家  ›  专栏  ›  技术社区  ›  Edward Strange

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

  •  5
  • Edward Strange  · 技术社区  · 14 年前

    我当然可以用 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();
    }
    

    我想把它还回去 true_ 而不是 false_ . 真正的例子有7个模板参数,大多数是默认的,使用boost.parameter按名称引用它们。为了使用 ISX基 我必须能够以某种方式拉出参数,而且在声明内部typedef的情况下,我找不到实现这一点的方法。

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

    1 回复  |  直到 14 年前
        1
  •  3
  •   Maxim Egorushkin    14 年前

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

    #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;
    }