一个 10代码 编译出错的问题!

#include

class Foo
{
public:
int add(int a, int b)
{
return a + b;
}
};
int main(void)
{
std::function<int(const Foo&, int, int)> func;
Foo a;
func = &Foo::add;

return 0;

}
这样为什么编译不过, 编译错误消息为
错误 2 error C2664: “void std::_Func_class<_Ret,const Foo &,int,int>::_Set(std::_Func_base<_Ret,const Foo &,int,int> *)”: 无法将参数 1 从“_Myimpl *”转换为“std::_Func_base<_Ret,const Foo &,int,int> *” d:\program files (x86)\microsoft visual studio 12.0\vc\include\functional 506 1

求教大神, 要怎么改?

如果换成这样, 就编译得过
int main(void)
{
std::function<int(Foo, int, int)> func;
Foo a;
func = &Foo::add;

return 0;

}
这是为什么

建议用std::bind函数。

谢谢, 已经解决!但还是有疑问, 为什么那样会编译不过

int add(int a, int b) const

亲, 那为什么这样也不可以呢?
int main(void)
{
std::function<int(Foo&, int, int)> func;
Foo a;
func = &Foo::add;

return 0;

}

总之,非const可以转换成const,但const不能自动转换成非const

#include

class Foo
{
public:
int add(int a, int b)const
{
return a + b;
}

};
int main(void)
{
std::function<int( Foo&, int, int)> func;
Foo a;
func = &Foo::add;

return 0;

}

我这样也还是编译不过, 加上引用就编译不过