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.h...): 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. And what's going on with std::min?
Hi Matthias: I am not really reading d_ary_heap.hpp actually. As my experiences the size_type is used as the parameter in template programming. It's not a confirmed type and it is depended on what you transmit the value of type while you build template function.
size_type comes from the allocator, and should be used for sizes and indices (well, that's how the STL seems to use them).
It could be "int, short, unsigned things even the customized type". It seems to be confirmed during compiling. In another hand, size_t is used "typedef", it is as a definition and will be confirmed during preprocessing.
As a typedef, compile time, after the preprocessor.
It has could be unsigned int, not always, but often. I hope it is helpful.
The indices should have the same type, and they should be size_type. The default size_type of allocators in the STL is size_t, so it usually isn't a problem, but for correctness it should be size_type. Ben