Alex Rivera | Logout

std::vector works with classes that are not default constructible?

Asked 2012-08-01T16:04:13.847
12

I've read in several places that std::vector requires it's template argument to be default constructible. Today I just tried it with one of my classes that has a deleted default constructor, and to my surprise it seems to be working just fine (with std::vector's default constructor). Is this portable behavior, or is this an implementation detail of gcc's STL and should I assume vector to require it's template argument to be default constructible?

Edit
Report

1 Answer

23

There are two vector<T> members that require a default constructible T in C++11:

explicit vector(size_type n);
void resize(size_type sz);

Nothing else does. So if you use these signatures, you need to have a default constructible type, else you do not.

answered 2012-08-01T17:10:07.887

Your Answer