Boost logo

Boost Users :

Subject: Re: [Boost-users] [heap] Mix use of size_t and size_type
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2015-03-11 19:06:46


On 12/03/2015 01:02, Ben Pope wrote:
> On Wednesday, March 11, 2015 12:09 PM, Cheng Mo wrote:
>> >Hi,
>> >While reading d_ary_heap.hpp from the heap library I was surprised to
>> >see a mix of size_t and size_type variables in an index computation
>> >(https://github.com/boostorg/heap/blob/master/include/boost/heap/d_ary_heap.hpp#L356):
>>
>> > size_type last_child_index(size_type index) const
>> > {
>> > const size_t first_index = first_child_index(index);
>> > const size_type last_index = (std::min)(first_index + D - 1,
>> >size() - 1);
>> > return last_index;
>> > }
>> >Isn't this a typo?
>
> Looks wrong to me, arithmetic should probably use the same types.

Agreed.

> And what's going on with std::min?

I'm not sure what D is, but that's a fairly basic "constrain to no later
than the end of the array" construct.

If you're referring to the parentheses around std::min itself, that's a
standard defense against #define min().

You might also see this in some code as "std::min
BOOST_PREVENT_MACRO_SUBSTITUTION (...)", which is more obvious but also
much uglier.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net