Make deque iterator operators hidden friends
François Dumont
frs.dumont@gmail.com
Thu May 9 05:29:00 GMT 2019
Thanks for the tip, nice to know.
Attached patch applied.
François
On 5/8/19 8:28 PM, Jonathan Wakely wrote:
> On 08/05/19 18:50 +0200, François Dumont wrote:
>> Here is a patch to reduce number of operators exposed at std
>> namespace scope.
>>
>> Â Â Â * include/bits/stl_deque.h
>> Â Â Â (_Deque_iterator<>::operator+(difference_type)): Make hidden friend.
>> Â Â Â (_Deque_iterator<>::operator-(difference_type)): Likewise.
>> Â Â Â (operator==(const _Deque_iterator<>&, const _Deque_iterator<>&)):
>> Â Â Â Likewise.
>> Â Â Â (operator!=(const _Deque_iterator<>&, const _Deque_iterator<>&)):
>> Â Â Â Likewise.
>> Â Â Â (operator<(const _Deque_iterator<>&, const _Deque_iterator<>&)):
>> Â Â Â Likewise.
>> Â Â Â (operator<=(const _Deque_iterator<>&, const _Deque_iterator<>&)):
>> Â Â Â Likewise.
>> Â Â Â (operator>(const _Deque_iterator<>&, const _Deque_iterator<>&)):
>> Â Â Â Likewise.
>> Â Â Â (operator>=(const _Deque_iterator<>&, const _Deque_iterator<>&)):
>> Â Â Â Likewise.
>>
>> Tested under Linux x86_64 normal/debug modes.
>>
>> Ok to commit ?
>>
>> François
>>
>
>> diff --git a/libstdc++-v3/include/bits/stl_deque.h
>> b/libstdc++-v3/include/bits/stl_deque.h
>> index 7a7a42aa903..72420c27646 100644
>> --- a/libstdc++-v3/include/bits/stl_deque.h
>> +++ b/libstdc++-v3/include/bits/stl_deque.h
>> @@ -238,24 +238,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
>> Â Â Â Â return *this;
>> Â Â Â Â Â }
>>
>> -Â Â Â Â Â _Self
>> -Â Â Â Â Â operator+(difference_type __n) const _GLIBCXX_NOEXCEPT
>> -Â Â Â Â Â {
>> -Â Â Â _Self __tmp = *this;
>> -Â Â Â return __tmp += __n;
>> -Â Â Â Â Â }
>> -
>> Â Â Â Â Â _Self&
>> Â Â Â Â Â operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
>> Â Â Â Â Â { return *this += -__n; }
>>
>> -Â Â Â Â Â _Self
>> -Â Â Â Â Â operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
>> -Â Â Â Â Â {
>> -Â Â Â _Self __tmp = *this;
>> -Â Â Â return __tmp -= __n;
>> -Â Â Â Â Â }
>> -
>
> These two are already not visible at namespace-scope, but it doesn't
> hurt to make them hidden friends for consistency.
>
>> +Â Â Â Â Â friend _Self
>> +Â Â Â Â Â operator+(const _Self& __x, difference_type __n)
>> _GLIBCXX_NOEXCEPT
>> +Â Â Â Â Â {
>> +Â Â Â _Self __tmp = __x;
>> +Â Â Â return __tmp += __n;
>
> I know you've just reused the original implementation, but it has a
> problem that we might as well fix while touching t his code.
>
> This function prevents NRVO copy elision, because __tmp += __n returns
> a reference, which gets copied into the return value.
>
> If you write it like this then the copy is elided, and __tmp is
> constructed directly in the return value slot:
>
> Â _Self __tmp = __x;
> Â __tmp += __n;
> Â return __tmp;
>
>
>> +Â Â Â Â Â friend _Self
>> +Â Â Â Â Â operator-(const _Self& __x, difference_type __n)
>> _GLIBCXX_NOEXCEPT
>> +Â Â Â Â Â {
>> +Â Â Â _Self __tmp = __x;
>> +Â Â Â return __tmp -= __n;
>
> Same problem here. It would be better to write:
>
> Â _Self __tmp = __x;
> Â __tmp -= __n;
> Â return __tmp;
>
> OK for trunk with those two changes, thanks.
>
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: deque_iterator.patch
Type: text/x-patch
Size: 11598 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20190509/b9d5d47f/attachment.bin>
More information about the Gcc-patches
mailing list