代码之家  ›  专栏  ›  技术社区  ›  abraham_hilbert

推导类的模板参数

  •  0
  • abraham_hilbert  · 技术社区  · 8 年前

    #include <type_traits>
    
    template< typename T >
    class A
    {};
    
    template< typename T >
    class B
    {};
    
    template< template <typename T> class GENERAL_t,  // Note: GENERAL_t is either A<T> or B<T>
              typename = std::enable_if_t< std::is_same<T,int>::value >
            >
    void foo( GENERAL_t a )
    {}
    

    错误消息:

    t.cpp:67:57: error: use of undeclared identifier 'T'
                  typename = std::enable_if_t< std::is_same<T,int>::value >
                                                            ^
    t.cpp:67:65: error: no type named 'value' in the global namespace
                  typename = std::enable_if_t< std::is_same<T,int>::value >
                                                                  ~~^
    t.cpp:69:15: error: use of class template 'GENERAL_t' requires template arguments
        void foo( GENERAL_t a )
                  ^
    t.cpp:66:43: note: template is declared here
        template< template <typename T> class GENERAL_t,  // Note: GENERAL_t is either A<T> or B<T>
                  ~~~~~~~~~~~~~~~~~~~~~       ^
    3 errors generated.
    

    foo 应该举个例子 class A class B T A B 是一个 int .

    2 回复  |  直到 8 年前
        1
  •  4
  •   bolov    8 年前
    • 你还没有申报 T

    • 删除 T template <class T> class GENERAL_t

    • GENERAL_t

    template<class T, template <class> class General_t, 
              class = std::enable_if_t< std::is_same<T,int>::value >
            >
    void foo(General_t<T> a)
    {}
    
        2
  •  4
  •   Barry    8 年前

    bolov's answer 在各个方面都是正确的。

    但在这种情况下,你不需要戴上SFINAE is_same

    template <template <class> class General>
    void foo(General<int> ) { }