This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: STL vector
- From: John Love-Jensen <eljay at adobe dot com>
- To: Mws <mws at twisted-brains dot org>, MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Mon, 31 Oct 2005 06:49:13 -0500
- Subject: Re: STL vector
Hi marcel,
> can someone explain it to me?
The *STANDARD* says that pop_back on an empty std::vector has an undefined
effect.
The safer version you suggest complies with the standard -- but it's not
reliable behavior on all C++ compilers, and it has a performance penalty for
code that does not expect nor rely upon the safe behavior you've suggested.
Even though the performance penalty is slight, it is enough that certain
situations may opt not to use std::vector because of the performance
overhead.
You can get that same safe behavior by doing a if(v.size()) v.pop_back(); in
your code. Or better yet, create a marcel::vector template class that adds
the additional safety feature you desire *AND* is cross-platform compatible.
That same guarantee cannot be made of the std:: namespace's implementation
of std::vector.
HTH,
--Eljay