c++gccc++20libstdc++std-ranges

Questions about ranges::distance implementation


I'm reading ranges::distance source and don't get the idea that:

I know that a final class has no derived classes, and the unary & is to get a pointer (or something alike) of an object. But I can't comprehend what they really do here with ranges::distance.

Same case for ranges::advance/prev/next, while other functors don't have such details, e.g. ranges::begin.


Solution

  • ranges::next is a so-called niebloid, unlike customization point objects (CPO), they are not function objects.

    To mimic their characteristics and prevent them from being misused as objects, this commit in libstdc++ added final and deleted oeprator& to them so that it is not possible to derive from them and take their addresses.

    On the other hand, ranges::begin is specified as CPO in the standard which is a function object that can be copied freely.

    There is also a paper P3136: Retiring niebloids that turns niebloids into CPOs to remove their annoying non-object nature.