I'm getting this weird error when inserting an element into std::unordered_map using emplace function but not if I use operator[] function overload. This is my code:
#include <iostream> #include <unordered_map> #include <memory> class B { public: B() = default; ~B() = default; }; class A { public: A() = default; ~A() = default; std::unique_ptr<B> b_ptr; }; int main() { std::unordered_map<std::string, A> mp; // mp.emplace("abc", A()); // gives compiler here auto& a = mp["def"]; } I'm getting huge error print when compiled. This is a short note of error: template argument deduction/substitution failed
std::pair<const std::string, A> p { "abc", A{} };. Adding move constructor intoAcan resolve it:A(A&&) = default;. Live demo: godbolt.org/z/5rEYcdro6.emplacecreateclass Aobject in place when inserting?std::pair<const std::string,A>in your case.