Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507085

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?

Upvotes: 6

Views: 1144

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153915

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!

Upvotes: 3

Related Questions