4

I'm trying to emplace values into a std::unordered map like so:

std::unordered_map<std::string, std::pair<std::string, std::string>> testmap; testmap.emplace("a", "b", "c")); 

which doesn't work, due to:

error C2661: 'std::pair::pair' : no overloaded function takes 3 arguments

I've looked at this answer and this answer, and it seems like I need to incorporate std::piecewise_construct into the emplacement to get it to work, but I don't think I quite know where to put it in this case. Trying things like

testmap.emplace(std::piecewise_construct, "a", std::piecewise_construct, "b", "c"); // fails testmap.emplace(std::piecewise_construct, "a", "b", "c"); // fails testmap.emplace(std::piecewise_construct, "a", std::pair<std::string, std::string>( std::piecewise_construct, "b", "c")); // fails 

Is there some way I can get these values to emplace?

I'm compiling with msvc2013 in case that matters.

0

1 Answer 1

5

You need to use std::piecewise_construct and std::forward_as_tuple for the arguments.

The following does compile:

#include <unordered_map> int main() { std::unordered_map<std::string,std::pair<std::string,std::string>> testmap; testmap.emplace(std::piecewise_construct,std::forward_as_tuple("a"),std::forward_as_tuple("b","c")); return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.