range and algorithm-related stuff

Denis Koroskin 2korden at gmail.com
Tue Jan 27 01:19:34 PST 2009


On Tue, 27 Jan 2009 05:13:31 +0300, Steven Schveighoffer <schveiguy at yahoo.com> wrote:

> "Denis Koroskin" wrote
>> On Mon, 26 Jan 2009 23:37:25 +0300, Denis Koroskin <2korden at gmail.com>
>> wrote:
>>> Checking if a range is empty() prior to accessing its head is useful.  
>>> If
>>> empty() is const, you can't do that.
>>
>> Err.. if empty() is not const and you have a const range reference.
>
> empty not being const does not imply that you can't access a const  
> member.
> Empty not being const allows all access.  The question is whether a  
> wrapping
> range (or any range for that matter) should implement empty as const, as  
> an
> empty that IS const cannot call a non-const function of a member.
>
> -Steve
>
>

I didn't say non-const empty restricts any member access, did I?

My point is, if empty() returns false (and you can't know it because empty() is not const) you may get either an invalid result, access violation or an exception on member access:

void foo(Range)(const Range r)
{
    // is r empty? can I /safely/ access its head?
    // let's cross our fingers and try it
    // hoping that it is either not empty or throws an exception
    auto head = r.head;
    ...
}




More information about the Digitalmars-d mailing list