【发布时间】:2013-08-01 15:43:40
【问题描述】:
如何实现下面的重载方法调用
class Foo {
void bind(const int,boost::function<int (void)> f);
void bind(const int,boost::function<std::string (void)> f);
void bind(const int,boost::function<double (void)> f);
};
第一次尝试
SomeClass c;
Foo f;
f.bind(1,boost::bind(&SomeClass::getint,ref(c));
f.bind(1,boost::bind(&SomeClass::getstring,ref(c)));
f.bind(1,boost::bind(&SomeClass::getdouble,ref(c)));
然后我找到了possible answer,所以尝试了这个:-
f.bind(static_cast<void (Foo::*)(int,boost::function<int(void)>)>(1,boost::bind(&SomeClass::getint)));
看起来很丑但可能有用?
但给出错误
error C2440: 'static_cast' : cannot convert from 'boost::_bi::bind_t<R,F,L>' to 'void (__cdecl Foo::* )(int,boost::function<Signature>)'
我可以使这种重载工作的任何想法。我怀疑正在发生类型擦除,但编译器显然可以识别重载方法,因为 Foo.cpp 编译得很好
【问题讨论】: