const debacle

Janice Caron caron800 at googlemail.com
Sat Mar 22 04:38:17 PDT 2008


On 22/03/2008, e-t172 <e-t172 at akegroup.org> wrote:
> The problem of this solution is that you're basically using templates to
>  solve a problem which will probably affect a LOT of functions

True, but I don't see that as a problem.


> A better solution: each "const K" template construct is always compiled
>  in the three versions (or rather in one version, because the object code
>  for the three is identical).

I completely agree. The "const K" template construct could (should?)
be instantiated for all three cases.

Note, however, that it's not actually /guaranteed/ that all three
cases will be identical, because the function might have "static if"
in it. e.g.

    K(char)[] f(const K)(K(char)[] buffer)
    {
        static if (K == invariant)
        {
            return buffer;
        }
        else
        {
            return buffer.dup;
        }
    }

But even in these edge cases, I'd be happy for all three versions to
auto-instantiate. There are only three cases, after all, and the
linker can probably throw away any function that's not used.



More information about the Digitalmars-d mailing list