[libcxx-commits] [PATCH] D81425: [libc++] Don't force a conversion to difference_type in std::advance

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jun 8 13:52:05 PDT 2020


ldionne created this revision.
ldionne added a reviewer: mclow.lists.
Herald added subscribers: libcxx-commits, dexonsmith, jkorous.
Herald added a project: libc++.
Herald added a reviewer: libc++.
ldionne added a comment.

@mclow.lists  I'm curious to hear what you think about this patch.

I think it's weird to have a wide open template parameter like this in `std::distance`, but it appears to be what the Standard wants. And using `difference_type` leads to warnings when passing an unsigned type when `-Wsign-conversion` is enabled (which is where this change comes from).


The Standard documents the signature of std::advance as

  template <class Iter, class Distance>
  constexpr void advance(Iter& i, Distance n);

Furthermore, it does not appear to put any restriction on what the type
of Distance should be. While it is understood that it should usually
be std::iterator_traits::difference_type, I couldn't find any wording
that mandates that. Similarly, I couldn't find wording that forces the
distance to be a signed type.

This patch changes std::advance to accept any type in the second argument,
which appears to be what the Standard mandates. I think it would be better
to at least restrict it to integral types, but that's not what the Standard
appears to say.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81425

Files:
  libcxx/include/iterator
  libcxx/test/std/iterators/iterator.primitives/iterator.operations/advance.pass.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81425.269336.patch
Type: text/x-patch
Size: 6279 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200608/73e7505c/attachment-0001.bin>


More information about the libcxx-commits mailing list