小编Joh*_*ohn的帖子

为什么在此上下文中无法推导出模板参数?

任何人都可以解释为什么编译器(g ++,visual c ++)在这种情况下无法推断模板参数?

struct MyClass
{
    void Foo(int x)&  {}
    void Foo(int x)&& {}
};

template<typename T>
void CallFoo(void(T::*func)(int)&)
{
    //create instance and call func
}

int main()
{
   CallFoo(&MyClass::Foo); // Fails to deduce T
}
Run Code Online (Sandbox Code Playgroud)

为什么编译器不能将T推导为MyClass?这仅适用于ref限定符重载的方法.如果方法被const-ness或参数类型重载,一切正常.在这种情况下,似乎只有Clang才能推断T.

c++ templates member-function-pointers c++11 ref-qualifier

16
推荐指数
1
解决办法
696
查看次数