[cfe-dev] [patch] [libc++] missing difference_type in advance implementation

Gonzalo BG gonzalobg88 at gmail.com
Sun Mar 23 14:59:55 PDT 2014


My iterator is using as difference_type this user-defined integer (
https://github.com/gnzlbg/arithmetic_type) which can't be compared with int
and is only explicitly convertible to other types.

I just reproduced the exact error I was getting. It fails due to the lack
of operator< for my difference_type and int.

> I don't see any provision here for an iterator having a difference_type
that is a user-defined type.

I don't think the standard allows user-defined integer types with
extraneous conversion rules as difference_type either.

>I'd really, really like to see a failing test case that I can include in
the libc++ test suite.
Otherwise, someone might break it again in the future.

Since all integer types can be compared with int, one would need a custom
integer type to construct a test for this. I can provide a random access
counting range this week, whose counting iterators have this custom integer
as difference_type if there is interest for it.

The arguments behind this patch are weak. There are probably multiple
occurrences of similar constructs within the standard library. Using a
custom integer without implicit conversions would require that the types
match exactly (e.g. difference_type{0}), and that all type conversions are
made explicit (e.g. via a static_cast). I don't know if this is worth
pursuing due to its low ROI. If this would be worth pursuing, adding a
clang warning for mismatching integer types would probably be a better way
of ensuring this than run-time tests.


On Sat, Mar 22, 2014 at 4:58 PM, Marshall Clow <mclow.lists at gmail.com>wrote:

>
> On Mar 20, 2014, at 1:22 PM, Gonzalo BG <gonzalobg88 at gmail.com> wrote:
>
> On Thu, Mar 20, 2014 at 9:09 PM, Joerg Sonnenberger <
> joerg at britannica.bec.de> wrote:
>
>> Do you have a test case that failed before?
>
>
> I have an iterator with a difference_type that is only explicitly
> convertible from int (not implicitly) and thus fails. Its implementation
> depends on Boost.Iterator (and everything that Boost.Iterator depends on)
> so I don't think it would be suitable for a test-case.
>
>
> I'm also wondering about this.
>
> The standard says that an iterator's difference_type must be a 'signed
> integer type" section 24.2.1 [iterator.requirements.general]
>
> These types are defined in section 3.9.1 [basic.fundamental], and seems to
> me to imply that it has to be either one of  "signed char", "short int",
> "int", "long int", and "long long int", or an "extended signed integer
> type" provided by the implementation (which means the compiler and/or
> standard library). I don't see any provision here for an iterator having a
> difference_type that is a user-defined type.
>
> Off to read the Boost.Iterator documentation....
>
> -- Marshall
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140323/2d64c776/attachment.html>


More information about the cfe-dev mailing list