Boost logo

Boost :

From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2019-12-05 14:35:35

Peter Dimov wrote:
> - a function that checks whether the remaining capacity is at least n should
> be added. I suggest the name
> bool can_fit( std::size_t n ) const noexcept;
> although my previous choice of "has_capacity_for" has merit too.
> This is, in my opinion, an absolutely necessary addition, because of my
> already-stated observation that the check is almost always gotten wrong.
> Here for instance is an example:
> if(size() + count > max_size())
> BOOST_FIXED_STRING_THROW(std::length_error{
> "size() + count > max_size()"});

Make this a free function template and you can use it with other containers:

template <typename CONTAINER>
bool can_fit( const CONTAINER& C, typename CONTAINER::size_type n ) noexcept
   return C.capacity() - C.size() >= n;

Boost list run by bdawes at, gregod at, cpdaniel at, john at