Any word on the return-type const syntax?

Walter Bright newshound1 at digitalmars.com
Sat Dec 8 14:25:03 PST 2007


Janice Caron wrote:
> On 12/8/07, Walter Bright <newshound1 at digitalmars.com> wrote:
>> Janice Caron wrote:
>>> Here's a typical problem.
>>>
>>>     class MyArray(T)
>>>     {
>>>         T[] a;
>>>
>>>         T* ptr() { return a.ptr; }
>>>         const(T)* ptr() const { return a.ptr; }
>>>         invariant(T)* ptr() invariant { return a.ptr; }
>>>     }
>>>
>>> A template helps how, exactly?
>>         TransferConst!(U,T) ptr(this U)() { return a.ptr; }
> 
> There are several things I don't understand about this example. You
> seem to be suggesting that
> 
>     TransferConst!(U,T)
> 
> resolves to
> 
>     K(T)*

I should have written:
	TransferConst!(U,T*)


> The next thing I don't understand is the notation
> 
>     ptr(this U)()
> 
> How does that even parse? I'm not familiar with this means of
> specifying template parameters. What does it mean?

It means U is given the type of the 'this' pointer. This was added in 2.008.

> The third thing I don't understand is, I see no specication of the
> constancy of the member function itself. I expect to see either
> const-at-the-end, or const-at-the-start, but I see neither. Where is
> it?

It's in wherever the 'this' pointer came from, i.e. from the caller.

> And of course, my final question has to be, is this function still
> virtual?

No.

> I think my way looks most readable, but you tell me.

The larger the function body is, the less readable and more error prone 
duplicating it becomes.

> One really important point to consider is that you need EXACTLY ONE
> instantiation of the function. That is, the bytes of machine code
> which comprise the function would be identical in all three cases, and
> therefore need to exist only once. I suspect that your template idea,
> in addition to robbing me of virtuality, would also instantiate the
> function three times.

Currently, it would instantiate it three times. This is a more general 
problem, though, and there are many other instances where it happens. 
Eventually, the best solution should be in the linker being able to 
coalesce identical comdat sections.



More information about the Digitalmars-d mailing list