Which one(s) of the following std::vector's member functions has the possibility/authority to reduce the vector's capacity?

70 views
Skip to first unread message

goodb...@googlemail.com

unread,
Mar 10, 2014, 3:15:23 AM3/10/14
to

For C++11, I have 4 member functions in question:
template <class T, class Allocator = allocator<T> >
class vector {
public:
vector<T,Allocator>& operator=(const vector<T,Allocator>& x);
vector<T,Allocator>& operator=(vector<T,Allocator>&& x);
vector& operator=(initializer_list<T>);
void clear() noexcept;
};

For C++03, I have 2 member function in question:
template <class T, class Allocator = allocator<T> >
class vector {
public:
vector<T,Allocator>& operator=(const vector<T,Allocator>& x);
void clear();
};

Among these member functions, I'm quite confident about the last one,
vector::clear() in C++03, because it's defined in terms of
erase(begin(),end()) which is not allowed to change the capacity. For
the others, I can't find myself a reliable answer, so I'm asking for
help here. Relevant excerpts from the standard are highly
appreciated.

Thanks,
goodbyeera


--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp...@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

James Kuyper

unread,
Mar 11, 2014, 11:16:55 AM3/11/14
to
The Subject: header of a message should contain a summary of the
message, it shouldn't be the sole location containing some key part of
the message. Your Subject: was

> Which one(s) of the following std::vector's member functions has the possibility/authority to reduce the vector's capacity?


On 03/10/2014 03:15 AM, goodb...@googlemail.com wrote:
>
> For C++11, I have 4 member functions in question:
> template <class T, class Allocator = allocator<T> >
> class vector {
> public:
> vector<T,Allocator>& operator=(const vector<T,Allocator>& x);
> vector<T,Allocator>& operator=(vector<T,Allocator>&& x);
> vector& operator=(initializer_list<T>);
> void clear() noexcept;
> };
>
> For C++03, I have 2 member function in question:
> template <class T, class Allocator = allocator<T> >
> class vector {
> public:
> vector<T,Allocator>& operator=(const vector<T,Allocator>& x);
> void clear();
> };
>
> Among these member functions, I'm quite confident about the last one,
> vector::clear() in C++03, because it's defined in terms of
> erase(begin(),end()) which is not allowed to change the capacity. For
> the others, I can't find myself a reliable answer, so I'm asking for
> help here. Relevant excerpts from the standard are highly
> appreciated.

The only guarantees the standard makes about the capacity are those
implicit in the definition of capacity(): "Returns: The total number of
elements that the vector can hold without requiring reallocation."
(23.3.6.3p1), which implies that capacity() >= size(), and "It is
guaranteed that no reallocation takes place during insertions that
happen after a call to reserve() until the time when an insertion would
make the size of the vector greater than the value of capacity()."
(32.3.6.3p5).

Therefore, I think that capacity() can shrink at any time if it is
currently greater than size(), and no reservation is currently in
effect. In addition, two functions are explicitly stated to have the
effect of changing the capacity: shrink_to_fit() and swap() - I presume
that both of these override any applicable reservations.

The value returned by capacity() would be more useful if it were
guaranteed to remain unchanged unless certain types of events occur,
such as those that allow a reallocation. Such a restricting could be
inferred from the fact that there's no reason for the capacity to change
spontaneously unless there's been a reallocation - but it's not clear to
me that the standard ever explicitly says so.
--
James Kuyper
Reply all
Reply to author
Forward
0 new messages