[ create a new paste ] login | about

Link: http://codepad.org/vFVp4vmw    [ raw code | output | fork ]

C++, pasted on Jun 14:
typedef std::map<int, boost::shared_ptr<std::string> > MapType;
typedef MapType::mapped_type ValueType;

template<typename F>
ValueType& get(MapType& map, int tag, F f) {
	std::pair<MapType::iterator, bool> pair = map.insert(MapType::value_type(tag, ValueType()));
	ValueType& value = pair.first->second;
	if (pair.second) {
		f(value);
	}
	return value;
}


struct InitNewString {
	void operator() (ValueType& value) {
		value.reset(new std::string());
	}
};
ValueType& get(MapType& map, int tag) {
	return get(map, tag, InitNewString());
}

int main(int, char**) {
	MapType map;
	*get(map, 1) = "test code.";
	std::cout << get(map, 1) << ":" << *get(map, 1) << std::endl;
	return 0;
}


Output:
1
0x8055478:test code.


Create a new paste based on this one


Comments: