Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While explicitly instantiating vector<someType>, what is the someType default constructor used for?

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> ?

like image 804
Yue Wang Avatar asked Feb 03 '14 10:02

Yue Wang


People also ask

Is the default constructor always called in C++?

A constructor is automatically called when an object is created. It must be placed in public section of class. If we do not specify a constructor, C++ compiler generates a default constructor for object (expects no parameters and has an empty body).

Is the default constructor always called?

This constructor is called the default constructor because it is run "by default;" if there is no initializer, then this constructor is used. The default constructor is used regardless of where a variable is defined.

How to declare vector in c++ class?

Syntax for Vectors in C++Every new vector must be declared starting with the vector keyword. This is followed by angle brackets which contain the the type of data the vector can accept like strings, integers, and so on.


2 Answers

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 :-)

like image 121
Steve Jessop Avatar answered Oct 26 '22 17:10

Steve Jessop


While explicitly instantiating std::vector<someType>, what is the someType default constructor used for?

It's used to construct the elements of the array when resizing the std::vector. For example:

std::vector<T> vector(10);

will default construct 10 elements of type T.

like image 33
Shoe Avatar answered Oct 26 '22 16:10

Shoe