C++ Logo

sg20

Advanced search

Re: [SG20] A draft paper to fix the range-based for loop to make it teachable

From: Peter C++ <peter.cpp_at_[hidden]>
Date: Fri, 13 Nov 2020 09:06:07 +0100
see below

Sent from Peter Sommerlad's iPad
+41 79 432 23 32

> On 13 Nov 2020, at 08:49, Nicolai Josuttis via SG20 <sg20_at_[hidden]> wrote:
>
>> One more technical note. Do you think that with your change proposed,
>> one could also think of dropping the deleted rvalue reference overload
>> of std::as_const? (And possibly similar other functions.) As far as I
>> understand, with your proposed changes, something like
>>
>>> for (auto & elem : std::as_const(getVector())) { ~~~ }
>>
>> would now work. Is it worth including it?
>>
> THAT is an interesting example, thanks.
> In principle, you are right, but exactly to avoid problems like the one
> above as_const() is deleted for temporary objects:
>
>> template <class T> void as_const(const T&&) = delete;
>
> So, while this could work now, it still would not compile.

FWIW, this is the approach other library functionality should consider to reduce the risk of dangling. either delete the temporary case, or think hard about a non-dangling return type, e.g., by value, if at all. may be we can find a solution to enable such a situation where it is obvious the programmer guarantees the lifetime of the temporary, but I am waiting for an insight, how to approach it.

regards
peter

Received on 2020-11-13 02:06:13