我正在编写一种类似于这样的封装器:
#include <iostream>
template<class T, class Value>
void Apply(void (T::*cb)(Value), T* obj, Value v)
{
(obj->*cb)(v);
}
class Foo
{
public:
void MyFunc(const int& i)
{
std::cout << i << std::endl;
}
const int& GetValue()
{
return i_;
}
private:
int i_ = 14;
};
int main()
{
Foo f;
Apply(&Foo::MyFunc, &f, f.GetValue());
}
我遇到了以下错误:
Apply
: 找不到匹配的重载函数。void Apply(void (__thiscall T::* )(Value),T *,Value)
: 模板参数Value
存在歧义,可能是int
或const int &
。void Apply(void (__thiscall T::* )(Value),T *,Value)
: 无法从const int
中推断出模板参数Value
。
因此,我知道这是由于模板参数推导引起的错误,但我不明白原因。为什么Value
两次都不会被解释为const int&
呢?