繁体   English   中英

使用成员函数作为模板参数时的c ++编译错误

[英]c++ compiling error when using member functions as a template parameters

我正在尝试将成员函数指针作为模板参数传递。 这是代码:

template <typename Ret, typename T, Ret(T::*mptr)()>
Handle<Value> get_value (Local<String> name, const AccessorInfo& info)
{
    ...
}

template <typename Ret, typename T>
void mbind (const char* name, Ret (T::*mptr)())
{
    ....
    objectTemplate->SetAccessor (String::NewSymbol (name),get_value<Ret,T,mptr>);
}

这是我得到的错误:

wrapper.h:184:5: error: ‘mptr’ is not a valid template argument for type ‘int (Cell::*)()’
wrapper.h:184:5: error: it must be a pointer-to-member of the form `&X::Y'
...

据我所知,指向成员函数的指针是有效的模板参数。 我不明白前面的代码有什么问题。 我使用的编译器是Ubuntu下的g ++ 4.5.2。

提前致谢。

更新:

似乎代码应该是错误的,因为mptr是运行时变量。 另一方面,先前的代码摘录进行了编译:

http://ideone.com/cv8pq

所以...对吗? 它取决于编译器吗?

mptr是运行时变量-您不能将其作为模板参数使用。 检查http://ideone.com/CIL4C

编辑

奇怪的是http://ideone.com/cv8pq ,类似于您的代码的代码可以成功编译并运行。

mbind应该已经有一个模板参数:

template <typename Ret, typename T, Ret (T::*mptr)()> 
void mbind (const char* name) {
    objectTemplate->SetAccessor (String::NewSymbol (name),get_value<Ret,T,mptr>()); 
}

PS:您忘记了get_value<>之后的()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM