Given the following code, I was surprised that try_emplace
could not work out to use the default constructor demonstrated in the first line of the main function, instead complaining there is no matching function call to Element::Element(double, double)
. Have I misunderstood the way the compiler creates default constructors or the usage of try_emplace
? I can of course get this code to work by defining an all parameter ctors for Element
, but this seems redundant.
#include <string>
#include <map>
struct Element
{
double a;
double b;
};
int main(int argc, char** argv)
{
Element e {2.0, 3.0};
std::map<std::string, Element> my_map;
my_map.try_emplace("hello", 2.0, 3.0);
return 0;
}