internal/local template memebers

Bill Baxter wbaxter at gmail.com
Fri Oct 24 21:25:18 PDT 2008


On Sat, Oct 25, 2008 at 1:05 PM, BCS <ao at pathlink.com> wrote:
> A number of times while working with template code I have found that I need
> a variable inside a template but don't want the side effects of doing so.
> Specifically 1) having a tmp variable results in having to explicitly
> reference template members rather than being able to use the default member
> rule and 2) the extra symbols seem to result in a substantial increases in
> the compile time memory usage.
>
> The idea I'm floating would be to have a "local" storage class (or whatever
> keyword is chosen) that would only be accessible from within the template it
> is declared in:
>
>
> template Foo(char[] str)
> {
>   local char[] first = str[0..$/2];
>   local char[] last = str[$/2..$];
>
>   const char[] Foo = Foo!(first) ~ Foo!(last); // legal; note not
> "Foo!().Foo"
>   // const char[] Foo = Foo!(first).a ~ Foo!(last).a; // error a is not
> accessible
> }
>
> During compilation these variables would be computed, used and thrown away
> resulting in no long term memory cost.

Indeed.  You're resurrecting a classic.  Maybe a good time to do so,
though.  Lots of uses for this suggestion -- internal helper
functions, local aliases for unwieldy types.  I've wished for it many
times, too.

Another way to go would just be to say if you have a symbol with the
same name as the template inside, then it automatically means
everything else is hidden.

---
Another gripe is having to repeat the exact template name over and
over in code like this.

This
template ThisIsTheTemplateNameFoo(char[] str)
{
  const char[] ThisIsTheTemplateName =
ThisIsTheTemplateName!(str[0..$/2]) ~
ThisIsTheTemplateName!(str[$/2..$]);
}

I think "this" for constructors has taught us that not repeating the
name of something when the compiler already knows it is a "honking
great idea".  It should be extended to templates somehow too.

--bb



More information about the Digitalmars-d mailing list