Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vector::resize(size_type sz, T c = T());

2 views
Skip to first unread message

subramanian100in@yahoo.com, India

unread,
Oct 27, 2009, 3:17:05 PM10/27/09
to
In the ISO/IEC 14882:2003 document, in page 490 in section 23.2.4, the
definition of the class template vector contains the following member
function declaration:
void resize(size_type sz, T c = T());

My question here is: why the second parameter type is just 'T' instead
of 'const T&' ie why doesn't the Standard declare this member function
as
void resize(size_type, const T& c = T());

But the following other member functions take 'const T&' type
parameter.

explicit vector(size_type n, const T& value = T(),
const Allocator& = Allocator());

void assign(size_type n, const T& u);
void push_back(const T& x);
iterator insert(iterator position, const T& x);

Kindly explain the reason.

Thanks
V.Subramanian

--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Daniel Krügler

unread,
Oct 27, 2009, 7:27:42 PM10/27/09
to
On 27 Okt., 20:17, "subramanian10...@yahoo.com, India"

<subramanian10...@yahoo.com> wrote:
> In the ISO/IEC 14882:2003 document, in page 490 in section 23.2.4, the
> definition of the class template vector contains the following member
> function declaration:
> void resize(size_type sz, T c = T());
>
> My question here is: why the second parameter type is just 'T' instead
> of 'const T&' ie why doesn't the Standard declare this member function
> as
> void resize(size_type, const T& c = T());
>
> But the following other member functions take 'const T&' type
> parameter.
>
> explicit vector(size_type n, const T& value = T(),
> const Allocator& = Allocator());
>
> void assign(size_type n, const T& u);
> void push_back(const T& x);
> iterator insert(iterator position, const T& x);
>
> Kindly explain the reason.

I assume that the original intend was to prevent an implementation
to perform tests whether the provided argument would be a member
of the container, because in this case the resizing step would
invalidate the reference before having copied it. It turns out that
this
logic - if at all - was unwarranted because the effect clause of
resize
is already specified as

if (sz > size())
insert(end(), sz-size(), c);
else if (sz < size())
erase(begin()+sz, end());
else
;

and the involved insert overload has the signature

void insert(iterator position, size_type n, const T& x);

so the corresponding problem would have to be solved by
insert anyway. And they need to do that for push_back
for the other insert overload you mentioned as well.

For these reasons the signature has been changed
in the current working draft as part of the defect report

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#679

to the expected one:

void resize(size_type sz, const T& c);

HTH & Greetings from Bremen,

Daniel Kr�gler

0 new messages