Range question

Mantis mail.mantis.88 at gmail.com
Fri Feb 17 22:02:15 PST 2012


18.02.2012 7:51, H. S. Teoh пишет:
> On Sat, Feb 18, 2012 at 05:19:52AM +0200, Mantis wrote:
>> 18.02.2012 2:50, H. S. Teoh пишет:
>>> ...
>> You cannot have ref local variable, so e is a copy in any case. It
>> may be a class reference or a pointer, so calling potentially
>> non-const methods is probably not safe here, but assignment
>> shouldn't give you problems.
> But that's the problem, if e is a dynamic array, then it can potentially
> be modified through the original reference after being assigned.
>
> Ideally, I'd need e to be a reference to an immutable type. But that
> requires a way of converting an arbitrary type to its immutable form,
> which I don't know how to do in a generic way.
>
>
> T
>
I see. But you can't have a generic copy operation either, due to 
possibly complicated memory model of your program. You'd need a proper 
copy construction for that, but there is no way to check for it's 
correctness in generic type. I'd just use constraint to limit a range 
underlying type to immutable, something like:

template isImmutable(T) {
     static if( is( T == immutable T ) ) {
         enum isImmutable = 1;
     } else {
         enum isImmutable = 0;
     }
}
...
if( isImmutable!(ElementType!T) )
...


More information about the Digitalmars-d-learn mailing list