了解“模板参数无效”错误信息 [英] Understanding a "template argument is invalid" error message

查看:856
本文介绍了了解“模板参数无效”错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑代码:

#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的特定定义。现在,我在GCC 4.5.3上遇到此编译错误


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

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

这是什么意思?为什么我可以专门从事 decltype(Functor()。Invoke()),但不能在 decltype(Functor()。Invoke< void> )) c>

What does it mean? Why can I specialize on decltype(Functor().Invoke()), but can not on decltype(Functor().Invoke<void>())?

推荐答案

您需要使用 code>因为解析模糊:

You need to qualify it with template because of parsing ambiguities:

 decltype(Functor().template Invoke<void>())
                    ^^^^^^^^

相关:在哪里和为什么我必须把模板和类型名称

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

Also, consider using std::declval rather than Functor() constructor.

这篇关于了解“模板参数无效”错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆