#include <iostream>
#include <string>
#include <map>
using namespace std;
class x{
public:
string s;
//x(){cout<<"default"<<endl;}
x(string s=""):s(s){cout<<"mine"<<endl;}
};
int main()
{
map<int,x> m;
m.insert(pair<int,x>(1,x("me")));
cout<<m[1].s<<endl;
}
I understand that map::operator[] requires default cst for when element is not present, but
m[1] gives error without default cst for class x in apple clang, even though the element for m[1] is already present. WHY?
I understand that map::operator[] requires default cst for when element is not present, [...]
That's not quite correct.
map::operator[]
always requires a default constructor because the element might not be present. Whether it actually is present is not that relevant, because whether a map<T>
has a operator[]
is decided at compile time already.