2012-11-12 73 views
4

考慮代碼:瞭解「模板參數無效」錯誤消息

#include <type_traits> 
#include <iostream> 

struct test1 { 
    void Invoke() {}; 
}; 

struct test2 { 
    template<typename> void Invoke() {}; 
}; 


enum class InvokableKind { 
    NOT_INVOKABLE, 
    INVOKABLE_FUNCTION, 
    INVOKABLE_FUNCTION_TEMPLATE 
}; 

template<typename Functor, class Enable = void> 
struct get_invokable_kind { 
    const static InvokableKind value = InvokableKind::NOT_INVOKABLE; 
}; 

template<typename Functor> 
struct get_invokable_kind< 
    Functor, 
    decltype(Functor().Invoke()) 
    > 
{ 
    const static InvokableKind value = InvokableKind::INVOKABLE_FUNCTION; 
}; 

template<typename Functor> 
struct get_invokable_kind< 
    Functor, 
    decltype(Functor().Invoke<void>()) 
    > 
{ 
    const static InvokableKind value = InvokableKind::INVOKABLE_FUNCTION_TEMPLATE; 
}; 


int main() { 
    using namespace std; 

    cout << (get_invokable_kind<test1>::value == InvokableKind::INVOKABLE_FUNCTION) << endl; 
    cout << (get_invokable_kind<test2>::value == InvokableKind::INVOKABLE_FUNCTION_TEMPLATE) << endl; 

} 

我試圖做的是創建一個元函數用於測試「invokability」的具體定義。而現在我卡在這個compilation error在GCC 4.5.3:

prog.cpp:37:3: error: template argument 2 is invalid

是什麼意思?爲什麼我可以專注於decltype(Functor().Invoke()),但不能在decltype(Functor().Invoke<void>())

回答