【发布时间】:2011-05-08 18:03:12
【问题描述】:
以下代码运行良好
#include <functional>
using namespace std;
using namespace std::placeholders;
class A
{
int operator()( int i, int j ) { return i - j; }
};
A a;
auto aBind = bind( &A::operator(), ref(a), _2, _1 );
这不是
#include <functional>
using namespace std;
using namespace std::placeholders;
class A
{
int operator()( int i, int j ) { return i - j; }
int operator()( int i ) { return -i; }
};
A a;
auto aBind = bind( &A::operator(), ref(a), _2, _1 );
我已经尝试过使用语法来尝试并明确地解决我想要的代码中的哪个函数到目前为止如果没有运气就无法工作。如何编写绑定行以选择采用两个整数参数的调用?
【问题讨论】:
-
A::operator()不是指单个函数,而是指一系列函数:我认为您必须对其进行强制转换才能“选择”正确的重载。由于我不熟悉 C++0x 并且我可能不知道更优雅的解决方案,因此我不会将此作为答案进行验证。 -
我写了an answer to a similar question,展示了三种将参数强制为正确类型的方法。
标签: c++ c++11 functional-programming std