c++ -::func has not been declared 错误

标签 c++ templates c++11

我想做的基本上是能够调用一个函数并让它向 lua 注册一个 c++ 函数。基本上类似于 registerFunction(function)。现在,我知道有些库可以为您执行此操作,但我想了解有人会如何编写这样的库。

我目前的方法是使用模板为传递给它的函数生成相关的胶水函数。

我的代码现在是这样的:

template<typename F>
struct registerStruct
{
    template<typename T>
    struct inner
    {
        static int func(lua_State*);
    };
};
template<typename F>
void registerFunction(const char *name,F function)
{
    lua_register(this->L,name,&registerStruct<decltype(function)>::inner<function>::func);
}

template<typename F>
struct registerStruct<F(void)> //I would write more classes for different numbers of arguments
{
    template<F(*T)(void)>
    struct inner
    {
        static int func(lua_State *L)
        {
            returnLua(L,T()); //push the return value onto lua's stack
            return 1;
        }
    };
};

然后我尝试这样使用它:

int test(void)
{
    std::cout<<"hello from c++"<<std::endl;
    return 0;
}

registerFunction("test",test);

用 gcc 编译产生错误::func has not been declared。

最佳答案

registerStruct<decltype(function)>::inner<function>::func

一方面,decltype(function)可以替换为 F .

此外,类模板 inner需要一个类型作为它的模板参数,并且 function是一个表达式,而不是一个类型。那可能应该是 ::inner<F> .

(或者可能 inner 根本不需要是模板。嵌套类 outer<T>::inner 仍然可以使用附加到其封闭类的模板参数 T。)

C++ 语法需要一些奇怪的东西来帮助解析器弄清楚模板的含义。通常 C++ 的含义取决于标识符是用于变量(对象或引用)、类型还是模板。但是经过 ::.->在模板中,如果运算符左侧的类型取决于模板参数,则不可能在初始解析期间找出下一个名称是哪个。如果您不帮忙,语言将假定该名称是一个变量。

所以C++认为registerStruct<decltype(function)>::inner是一个变量。然后我们有小于运算符 < ,后跟有效表达式 function ,然后是大于运算符 > , 然后 ::func .等等,没有什么叫做 ::func !

指定inner真的是一个模板,你需要

&registerStruct<F>::template inner<F>::func

关于c++ -::func has not been declared 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17056111/

相关文章:

c++ - gcc 奇怪的转换警告(从 ‘A<B>::count_type {aka short unsigned int}’ 转换到 ‘int’ 可能会改变它的值)

c++ - 具有不同保护级别的访问器

c++ - 如何将 C++ 类包装在基于 C 的 dll 或基于 CLI 的 dll 中?

c++ - 我可以使用 wm_copydata 模拟应用程序退出代码吗?

多种数据类型的 C++ 堆栈(RPN vector 计算器)

c++ - 没有 "using"的模板基类的访问属性

c++ - constexpr 静态成员变量的链接器错误

c++ - C++中函数指针的默认值

c++ - 为什么 "same signature"的模板和非模板函数重载调用非模板函数?

c++ - 对如何更新 Mingw 有一点帮助吗?