Any word on the return-type const syntax?

Janice Caron caron800 at googlemail.com
Sat Dec 8 13:08:51 PST 2007


On 12/8/07, Janice Caron <caron800 at googlemail.com> wrote:
> 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.

This is really key for me. A member function like

    K(T)[] f() { /*...*/ } K

where K stands for const, invariant, or nothing, needs exactly one
instantiation, not three, though it may generate three distinct APIs.
Also, there's no reason why it can't be virtual.

Templates would not only generate three distinct instantiations, they
would rob us of of virtuality.

Let's go back to the array example. I could write a /single/ function

    class MyArray(T)
    {
        T[] a;
        const(T)* ptr() const { return a.ptr; }
    }

with the knowledge that I could safely do

    MyArray!(int) a;
    int* ptr = cast(int*)a.ptr;

because I have a priori knowledge that a is not const. It should be
possible to encapsulate that knowledge in the function definition.



More information about the Digitalmars-d mailing list