Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stl container of references C++11

Tags:

c++

I know this question was asked a million of times. And most of answers just says that object should be CopyAssignable and CopyConstructible. But documentation clearly says this is the rule (until C++11)! But still it doesn't work. Why?

like image 532
nikitablack Avatar asked Nov 27 '22 07:11

nikitablack


2 Answers

Yes, the requirements are less strict now. However, right below the part you're referencing, the linked documentation clearly states that:

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements. (since C++11)

References don't meet the requirements of Erasable, therefore they still can't work.

like image 135
eerorika Avatar answered Dec 05 '22 10:12

eerorika


The rules apply until C++11. In C++11 is required that element type is a complete type and meets the requirements of Erasable in which case references are not.

However you can use std::reference_wrapper to wrap your references and store them in a vector.

std::vector<std::reference_wrapper<T>> vector_of_references;
like image 24
101010 Avatar answered Dec 05 '22 11:12

101010