This works fine:
#include <iostream>
#include <map>
using namespace std;
struct Bar
{
int i;
int f;
};
int main()
{
map<int, Bar> m;
Bar b;
b.i = 1;
b.f = 2;
m[0] = b;
}
But if I want to make it a little more concise, I get errors:
int main()
{
map<int, Bar> m;
m[0] = {1, 2};
}
Is there any way to make that struct initialization syntax work? Am I doing it wrong, or is it prohibited for maps?
You can add a constructor:
In this situation I would say it is better than the fancy new initializer as it actually lets the maintainer see what type is being put in the map without having to go and look for it.