This works:
Code: Select all
#include <map>
class A
{
public:
A(int _a, int _b) : a(_a), b(_b) {};
float a, b;
};
int main()
{
std::multimap<int, A> map;
map.emplace(12, A{ 2, 3 });
return 0;
}
When you call emplace with 3 parameters compiler does not know what to do with them
'std::pair<const int,A>::pair': no overloaded function takes 3 arguments
because emplace creates a pair, and to create a pair you need 2 parameters.
now
does not work
no instance of function template "std::multimap<_Kty, _Ty, _Pr, _Alloc>::emplace [with _Kty=int, _Ty=A, _Pr=std::less<int>, _Alloc=std::allocator<std::pair<const int, A>>]" matches the argument list
because (i guess) the compiler is not certain what object to construct with {}
If you tell explicitly what kind of pair you want to create - it works
Code: Select all
map.emplace(std::make_pair<int,A>(12, { 2, 3 }));
but now there is little point in using emplace, might as well just insert
Code: Select all
map.insert (std::make_pair<int, A>(12, { 2, 3 }));
another way to tell compiler what to create with {} is like this
and this is the best way i know