从成员函数模板化参数调用成员函数
鉴于以下代码我无法编译。
template < typename OT, typename KT, KT (OT::* KM)() const >
class X
{
public:
KT mfn( const OT & obj )
{
return obj.*(KM)(); // Error here.
}
};
class O
{
public:
int func() const
{
return 3;
}
};
int main( int c, char *v[] )
{
int a = 100;
X< O, int, &O::func > x;
O o;
std::cout << x.mfn( o ) << std::endl;
}
我收到以下错误消息,
error: must use '.*' or '->*' to call pointer-to-member function in '&O::func (...)'
我以为我正在使用 .* 但我显然出了问题。
如何调用成员函数?
我试过了
return obj.*(template KM)();
return obj.*template (KM)();
return obj.template *(KM)();
,没有一个有效。
Given the following code which I can't get to compile.
template < typename OT, typename KT, KT (OT::* KM)() const >
class X
{
public:
KT mfn( const OT & obj )
{
return obj.*(KM)(); // Error here.
}
};
class O
{
public:
int func() const
{
return 3;
}
};
int main( int c, char *v[] )
{
int a = 100;
X< O, int, &O::func > x;
O o;
std::cout << x.mfn( o ) << std::endl;
}
I get the folling error message
error: must use '.*' or '->*' to call pointer-to-member function in '&O::func (...)'
I thought I was using .* but I've obviously got something wrong.
How do I call the member function ?
I've tried
return obj.*(template KM)();
return obj.*template (KM)();
return obj.template *(KM)();
None of which worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的语法是
The correct syntax is