I have the following code:
class CEvent
{
public:
CEvent(std::string const&) {}
};
std::unordered_map<std::string, CEvent> m_messageList;
CEvent& GetMessageEvent(std::string const& name)
{
auto it = m_messageList.find(name);
if (it != m_messageList.end())
{
auto pair = m_messageList.emplace(std::piecewise_construct,
std::forward_as_tuple(name), // Copy-construct 'name' as the key
std::forward_as_tuple(name)); // Construct CEvent in-place with the name
retu pair.first->second;
}
retu it->second;
}
I think the code is pretty clean, but I don't like that I have to do a find separate from emplace. Is there a way to do this better? Or is this "good enough"? I know I could probably call emplace instead of find first, to accomplish both tasks, but this means creating a CEvent every time, even if no real insert happens.
