1
我创建地图一个函数指针如示于下面的最小工作实例:将新元素推入函数指针映射“inline”?
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef std::vector<bool > Bar;
typedef bool (*Foo)(Bar b);
typedef std::map<int, Foo > MapOfFunctions;
inline bool f1 (Bar b) { return b[0] && b[1]; }
int main() {
MapOfFunctions myMap;
myMap[0] = f1; // works
//myMap[1] = // Define a function right here, "in line"?!
if (myMap[0](Bar (2,true)))
cout << "it's true" << endl;
return 0;
}
我想知道是否有可能定义地图“内联”的新的元件(即功能) ,即在代码中,而不必先在代码的其他地方创建一个单独的函数(本例中为f1
)。
编辑:该解决方案应该最好是C++ 98。
怎么样C++ 98的解决方案?看起来这是一个C++ 11功能。对不起,在OP中没有提到这个要求。 –
@space_voyager:不,对不起。 –
@space_voyager,'boost :: function'包含'boost :: bind'结果或'Boost.Phoenix' lambas。 – chris