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;
}