如何区分重载函数的左值和右值成员函数指针

How do I differentiate an lvalue and rvalue member function pointer for overloaded functions?

本文关键字:函数 成员 指针 何区 重载      更新时间:2023-10-16

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

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);
}

我希望对成员功能做同样的事情。 但是,它似乎没有翻译:

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);
}

为什么?

我知道的另一种方法是对常规函数执行此操作:

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);
}

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

我猜这是一个视觉C++错误,因为以下代码(基本上是您在问题中的内容)在 gcc 和 clang 上为我编译,我认为没有理由期望它不这样做:

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
}

有关问题的另一部分,请参阅为什么C++中不存在对成员的引用?。