了解"模板参数无效"错误消息

uli*_*tko 4 c++ gcc sfinae template-meta-programming c++11

考虑一下代码:

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

}
Run Code Online (Sandbox Code Playgroud)

我要做的是创建一个元函数来测试"invokability"的特定定义.现在我在GCC 4.5.3上遇到了这个编译错误:

prog.cpp:37:3:错误:模板参数2无效

这是什么意思?为什么我可以专攻decltype(Functor().Invoke()),但不能上decltype(Functor().Invoke<void>())

Pub*_*bby 6

template由于解析歧义,您需要对其进行限定:

 decltype(Functor().template Invoke<void>())
                    ^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

相关:我必须在何处以及为何要使用"模板"和"typename"关键字?

另外,考虑使用std::declval而不是Functor()构造函数.