[PATCH] D49402: [STLExtras] Add size() for arrays

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 30 08:04:07 PDT 2018


I'm good with it - don't much mind whether it's reviewed separately or not
myself, so long as the commit messages, etc, are accurate of the final
form. I'd only suggest modifying the test somewhat - rather than testing
that array_lengthof is the same as size, I'd test that the size answers are
correct in absolute terms (ie: by hardcoding the answer: EXPECT_EQ(3,
size(a1)); etc...


On Mon, Jul 30, 2018 at 1:25 AM James Henderson <
jh7370.2008 at my.bristol.ac.uk> wrote:

> I think this would be worthwhile, personally. I'd create a separate review
> for it though, mentioning the original.
>
> James
>
> On 28 July 2018 at 01:46, Vedant Kumar <vsk at apple.com> wrote:
>
>> Neat! That seems to work:
>>
>> ```
>> diff --git a/llvm/include/llvm/ADT/STLExtras.h
>> b/llvm/include/llvm/ADT/STLExtras.h
>> index 94365dd9ced..47450623826 100644
>> --- a/llvm/include/llvm/ADT/STLExtras.h
>> +++ b/llvm/include/llvm/ADT/STLExtras.h
>> @@ -1044,11 +1044,11 @@ void erase_if(Container &C, UnaryPredicate P) {
>>  template <typename R>
>>  auto size(R &&Range, typename std::enable_if<
>>                           std::is_same<typename
>> std::iterator_traits<decltype(
>> -
>>  Range.begin())>::iterator_category,
>> +
>>  adl_begin(Range))>::iterator_category,
>>
>>  std::random_access_iterator_tag>::value,
>>                           void>::type * = nullptr)
>> -    -> decltype(std::distance(Range.begin(), Range.end())) {
>> -  return std::distance(Range.begin(), Range.end());
>> +    -> decltype(std::distance(adl_begin(Range), adl_end(Range))) {
>> +  return std::distance(adl_begin(Range), adl_end(Range));
>>  }
>>
>>
>>  //===----------------------------------------------------------------------===//
>> diff --git a/llvm/unittests/ADT/IteratorTest.cpp
>> b/llvm/unittests/ADT/IteratorTest.cpp
>> index 50c3b01bbc7..b335bdec766 100644
>> --- a/llvm/unittests/ADT/IteratorTest.cpp
>> +++ b/llvm/unittests/ADT/IteratorTest.cpp
>> @@ -382,9 +382,13 @@ TEST(ZipIteratorTest, Reverse) {
>>  TEST(RangeTest, Distance) {
>>    std::vector<int> v1;
>>    std::vector<int> v2{1, 2, 3};
>> +  int a1[] = {1, 2, 3};
>> +  int a2[5] = {0};
>>
>>    EXPECT_EQ(std::distance(v1.begin(), v1.end()), size(v1));
>>    EXPECT_EQ(std::distance(v2.begin(), v2.end()), size(v2));
>> +  EXPECT_EQ(array_lengthof(a1), size(a1));
>> +  EXPECT_EQ(array_lengthof(a2), size(a2));
>>  }
>>
>>  TEST(IteratorRangeTest, DropBegin) {
>> ```
>>
>> Do folks think this is worth supporting?
>>
>> vedant
>>
>> On Jul 23, 2018, at 4:23 PM, David Blaikie <dblaikie at gmail.com> wrote:
>>
>> Seems like the existing size(R &&Range) function could be generalized (by
>> using adl_begin/end rather than member begin/end? Would that be enough) to
>> cover arrays as well, maybe?
>>
>> On Wed, Jul 18, 2018 at 11:24 AM Paul Semel via Phabricator via
>> llvm-commits <llvm-commits at lists.llvm.org> wrote:
>>
>>> paulsemel closed this revision.
>>> paulsemel added a comment.
>>>
>>> I agree with James !
>>>
>>>
>>> Repository:
>>>   rL LLVM
>>>
>>> https://reviews.llvm.org/D49402
>>>
>>>
>>>
>>> _______________________________________________
>>> llvm-commits mailing list
>>> llvm-commits at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180730/533c2dcc/attachment.html>


More information about the llvm-commits mailing list