While explicitly instantiating vector<someType>, what is the someType default constructor used for?
c++, stl, templates, vector
Solution
C++11 introduces a new constructor, `vector(size_type n)`, and "obviously" that constructor can't be instantiated unless the `value_type` is default-constructible. It was `vector(size_type n, const T & = T(), const Allocator& alloc = Allocator())` in C++03, but they changed it to two separate constructors. A similar change was made to `resize()`.
In GCC and as far as I know also per the standard: the `vector` template can be at least implicitly instantiated without the elements being default constructible. Implicit template class instantiation only instantiates the member functions you use.
In C++03 only the default arguments use default construction. You can explicitly instantiate the template, but you won't be able to call those functions without providing a value for that parameter.
So I'm pretty sure that change is what prevents `vector<NoDefault>` being explicitly instantiated in C++11 (and I suppose the allocator type needs to be default-constructible too). But I might have missed something. It seems a little odd to make that change given that it introduces this backward incompatibility. There's probably a proposal somewhere that justifies it if you want a deeper reason why this no longer works as opposed to just why this doesn't work :-)
Problem
It's an exercise from C++ Primer 5th Edition: Exercise 16.26: Assuming NoDefault is a class that does not have a default constructor, can we explicitly instantiate `vector<NoDefault>`? If not, why not? P.677 I know it doesn't compile if class `NoDefault` has no default constructor, but can not explain the reason. Can anyone tell me how is the default constructor used while explicitly instantiating `template class std::vector<NoDefault>` ?