把boost::bind的返回值插入到std::unordered_map中

阅读量: searchstar 2020-05-03 12:56:26
Categories: Tags:
#include <iostream>
#include <unordered_map>

#include <boost/bind.hpp>

using namespace std;

struct A {
	void print(int x) {
		cout << x << endl;
	}
	void print2(int x) {
		cout << "2 " << x << endl;
	}
};
struct B {
	void print(long long x) {
		cout << x << endl;
	}
};
struct C {
	void print(int x) {
		cout << x << endl;
	}
	int sth;
};

int main() {
	unordered_map<int, decltype(boost::bind(&A::print, A(), int()))> ma;

	A a;
	int x = 2333;
	auto f = boost::bind(&A::print, a, x);
	//ma[1] = f;
	ma.insert(make_pair(2, f));
	x = 0;
	auto it = ma.find(2);
	if (it != ma.end()) {
		it->second();
	} else {
		cout << "not found\n";
	}

	long long llx = 23333;
	B b;
	auto g = boost::bind(&B::print, b, llx);
	llx = 0;
	//ma.insert(make_pair(3, g));

	x = 2333;
	C c;
	auto h = boost::bind(&C::print, c, x);
	x = 0;
	//ma.insert(make_pair(4, h));

	x = 23332;
	auto f2 = boost::bind(&A::print2, a, x);
	ma.insert(make_pair(5, f2));
	ma.find(5)->second();

	return 0;
}
2333
2 23332