我如何区分重载函数的左值和右值成员函数指针? [英] How do I differentiate an lvalue and rvalue member function pointer for overloaded functions?

查看:162
本文介绍了我如何区分重载函数的左值和右值成员函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以这样做来区分一个右值函数名和一个左值函数指针:

I know that I can do this to differentiate a rvalue function name and an lvalue function pointer:

template <typename RET_TYPE, typename...ARGs>
void takeFunction(RET_TYPE(& function)(ARGs...))
{
    cout << "RValue function" << endl;
}

template <typename RET_TYPE, typename...ARGs>
void takeFunction(RET_TYPE(*& function)(ARGs...))
{
    cout << "LValue function" << endl;
}

void function()
{
}

void testFn()
{
    void(*f)() = function;
    takeFunction(function);
    takeFunction(f);
}

我想对成员函数做同样的事情。但是,它似乎不翻译:

And I wish to do the same for member functions. However, it doesn't seem to translate:

struct S;
void takeMemberFunction(void(S::&function)()) // error C2589: '&' : illegal token on right side of '::'
{
    cout << "RValue member function" << endl;
}

void takeMemberFunction(void(S::*&function)())
{
    cout << "LValue member function" << endl;
}

struct S
{
    void memberFunction()
    {
    }
};

void testMemberFn()
{
    void(S::*mf)() = &S::memberFunction;
    takeMemberFunction(S::memberFunction);
    takeMemberFunction(mf);
}

为什么?

我知道的一个替代方法是为常规函数执行此操作:

An alternative I know is to do this for regular functions:

void takeFunction(void(*&& function)())
{
    cout << "RValue function" << endl;
}

void takeFunction(void(*& function)())
{
    cout << "LValue function" << endl;
}

void function()
{
}

void testFn()
{
    void(*f)() = function;
    takeFunction(&function);
    takeFunction(f);
}

这转换为成员函数:

struct S;
void takeMemberFunction(void(S::*&&function)())
{
    cout << "RValue member function" << endl;
}

void takeMemberFunction(void(S::*&function)())
{
    cout << "LValue member function" << endl;
}

struct S
{
    void memberFunction()
    {
    }
};

void testMemberFn()
{
    void(S::*mf)() = &S::memberFunction;
    takeMemberFunction(&S::memberFunction); // error C2664: 'void takeMemberFunction(void (__thiscall S::* &)(void))' : cannot convert argument 1 from 'void (__thiscall S::* )(void)' to 'void (__thiscall S::* &)(void)'
    takeMemberFunction(mf);
}

但我想知道我的第一个例子不翻译的差异。

But I would like to know the discrepancy for my first example not translating.

推荐答案

我猜这是一个Visual C ++错误,如下面的代码(基本上你在你的问题) a gref =http://coliru.stacked-crooked.com/a/3bef186b67063134 =nofollow>在gcc和clang上编译我,我没有理由期望它不会:

I'm guessing this is a Visual C++ bug, as the following code (basically what you have in your question) compiles for me on both gcc and clang, and I see no reason to expect it not to:

struct S;

void bar(void (S::*& f)() ) {
    std::cout << "lvalue" << std::endl;
}
void bar(void (S::*&& p)() ) {
    std::cout << "rvalue" << std::endl;
}

struct S {
    void foo() { }  
};

int main() {
    void (S::*f)();

    bar(f);        // prints lvalue
    bar(&S::foo);  // prints rvalue
}

对于问题的其他部分, =http://stackoverflow.com/q/21952386/2069064>为什么C ++中不存在引用到成员?。

For the other part of your question, see Why doesn't reference-to-member exist in C++?.

这篇关于我如何区分重载函数的左值和右值成员函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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