C++ call a member function of an anonymous object instance?


I have the following piece of code:

map<int, set<int>> my_map;
int my_int1, my_int2;
// some code
set<int> temp;
my_map.insert(make_pair(my_int1, temp));

Is there some way to avoid using the temp (at least just for the code to look nicer, not necessarily for performance) and use an anonymous object? Something like:

my_map.insert(make_pair(my_int1, (set<int>{}).insert(my_int2)));

Except, the above line doesn’t compile.

>Solution :

Not tested but try something like this:

my_map.insert({my_int1, {my_int2}});

Leave a ReplyCancel reply