【发布时间】:2017-03-22 16:09:05
【问题描述】:
我正在做一些看起来像这样的包装器:
#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&?
【问题讨论】:
-
这个例子是对问题的重现,但绝不是我正在编写的代码per-se。
标签: c++ templates ambiguous template-argument-deduction ambiguous-call