Template Declarations - Why not Template definitions?

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 30 16:23:53 PDT 2015


On Tuesday, 30 June 2015 at 21:06:58 UTC, WhatMeWorry wrote:
> All the stuff I've read about templates always refers to them 
> as template declarations.
>
> So with the following code segment:
>
> template codeBlockTemplate(T, U)
> {
>     T a = 7;
>     U b = 'z';
> }
>
> codeBlockTemplate!(int, char);  // error here
>
> Microsof's Visual Studio IDE tells me <identifier> expected, ; 
> found
>
> But aren't templates instantiated at compile time? If so, isn't 
> memory allocated at compile time, so in theory couldn't 
> templates support code definitions?

I'm having trouble understanding the question/problem, but maybe 
you're looking for `mixin`:

mixin codeBlockTemplate!(int, char);

>
>
> Bonus question: Isn't a Zero-parameter template declaration 
> pretty much worthless?

Pretty much, yeah. A function with an empty body is pretty much 
worthless, too. I see no point in adding extra logic to forbid 
them, though. They don't do any harm and there are probably cases 
when it'd be annoying were they forbidden.


More information about the Digitalmars-d-learn mailing list