Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::vector::reserve(0); legal?

Is std::vector::reserve(0); legal and what will it do?

like image 397
NPS Avatar asked Nov 07 '14 18:11

NPS


1 Answers

There's nothing to prohibit it. The effect of reserve is:

After reserve(), capacity() is greater or equal to the argument of reserve if reallocation happens; and equal to the previous value of capacity() otherwise. Reallocation happens at this point if and only if the current capacity is less than the argument of reserve().1

Since the value of capacity() can never be less than 0 (it's unsigned), this can never have any effect; it can never cause a reallocation.


1. c++ standard, [vector.capacity]

like image 77
Jerry Coffin Avatar answered Oct 16 '22 02:10

Jerry Coffin