I am using g++ to compile and the above program gives an error saying I can't perform the operation: foo[k] = a;
It says that it needs a constructor of kind A(). When I do provide a default constructor then program compiles good. Any idea why we need a default constructor here.

On the side note, if I create a std::pair<int, A> object and call insert method, it works good even if there isn't any default constructor for A. Eg.
std::pair<int, A> testPair(k, a);
foo.insert(testPair);

I first found this behavior with boost::unordered_map<>. Later found that even std::unordered_map behaves in the same way. Anyone knows the exact reason for the above behavior. Thanks in advance.

If you instead use the at member function, foo.at(k) = a;, you would not have to have a default constructor, but if the key doesn't exist in the map it will throw an exception instead. That would force you to insert the element before you can access it.