I have an unordered map of string-pairs that i reference via a message id (key). Now I want to construct the string pair in place using the temporary string objects that the function receives, hence reusing the memory already allocated for the string on the heap. But I'm having difficulty wrapping my head around this construction. Here is what I came up with:
std::unordered_map<int, std::pair<std::string, std::string>> data_map;
void foo(int msg_id, std::string&& topic, std::string&& data)
{
data_map.emplace(std::piecewise_construct, std::forward_as_tuple(msg_id), std::forward_as_tuple(std::piecewise_construct, std::forward_as_tuple(topic), std::forward_as_tuple(data)));
}
My reasoning says that I need to construct a first pair for the map containing key and value of which the value must be created by another piecewise pair constructor to which I feed the r-value refs. It compiles but my gut feeling tells me something is off. Will I be able to reuse the allocated heap memory of the string like this?