Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use reference types as the value type of a container type?

Tags:

c++

stl

For example, std::vector<int&> vec_int; this seems to be invalid in c++. Why is this invalid?

like image 910
Thomson Avatar asked Oct 18 '10 09:10

Thomson


1 Answers

STL containers need to be able to construct objects with default constructor. You cannot do that with a reference. A reference is guaranteed to be valid, therefore it always has to be initialized with assignment.

You need to use a pointer instead.

like image 54
ypnos Avatar answered Sep 18 '22 01:09

ypnos