Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do vector elements need to be movable?

I noticed that requirements of std::vector on its elememt type T changed from C++03 to C++0x. T now does not need to be copy constructible anymore, but move constructibility suffices.

Is it required for T even if we don't potentially reallocate?

vector<boost::scoped_ptr<int>> x(numberElements);

I don't see a need for a move here. What does the specification say?

like image 504
Johannes Schaub - litb Avatar asked Oct 28 '12 17:10

Johannes Schaub - litb


1 Answers

According to 23.3.6.2 [vector.cons] paragraph 4 the constructor you are using requires DefaultInsertable. Since the constructor isn't one of the constructors for the requirements table also asking for CopyInsertable there shouldn't be any additional requirements. According to 23.2.1 [container.requirements.general] paragraph 13, DefaultInsertable means that the following expression is well-formed:

allocator_traits<A>::construct(m, p);

It seems, this means that the answer depends on the used allocator A. I don't quite fancy to analyze the deeper meaning of 20.6.7.2 [allocator.uses.construction]. Let's do the instructor escape: Determining what this paragraph means is left as an exercise!

like image 185
Dietmar Kühl Avatar answered Oct 09 '22 06:10

Dietmar Kühl