www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template Declarations - Why not Template definitions?

reply "WhatMeWorry" <kheaser gmail.com> writes:
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?


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

Thanks.
Jun 30 2015
next sibling parent reply "anonymous" <anonymous example.com> writes:
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.
Jun 30 2015
parent "WhatMeWorry" <kc_heaser yahoo.com> writes:
I was reading "D Templates: A Tutorial" by Philippe Sigaud which 
says:

--------------------------- quote
What is a Template? In the next chapters, you’ll see how to 
define function, struct and class templates.

But before that, I’d like to introduce what a template really is, 
because this
definition is the most fundamental of the whole document. As I 
said, a template
is a way to define a blueprint to generate some code, be it a 
class definition, a
function or. . . what? What could be the most abstract unit of 
code?

But where is the basic unit to hold this code? Well, a code block 
of course, or a
scope.

This is what a template is, at its core: a named, parameterized,
code block, ready to be instantiated just for you.
--------------------------- end quote


so thinking that I had reached a first principle, I wrote the 
above template.
But then when this failed to compile, I then read further and it 
says:


--------------------------- quote
Template Declarations

Here is the syntax for a template declaration:

template templateName(list, of, parameters)
{
     // Some syntactically correct declarations here
     // The arguments are accessible inside the template scope.
}
--------------------------- end quote

so the code block must consist only of declarations.  Which lead 
to me to ask my original question.

mixins could work but I'm just trying to understand templates and 
this particular limitation.
Jun 30 2015
prev sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Tuesday, 30 June 2015 at 21:06:58 UTC, WhatMeWorry wrote:
 Bonus question: Isn't a Zero-parameter template declaration 
 pretty much worthless?
Functions in templates get certain attributes inferred automatically, like ` nogc`, `pure`, `nothrow`, ` safe`. Some people use them for that purpose. (Because of IFTI, functions with empty template parameters can be called with the same syntax as normal functions, they don't need the `!()`.)
Jul 01 2015
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Wednesday, July 01, 2015 09:29:56 via Digitalmars-d-learn wrote:
 On Tuesday, 30 June 2015 at 21:06:58 UTC, WhatMeWorry wrote:
 Bonus question: Isn't a Zero-parameter template declaration
 pretty much worthless?
Functions in templates get certain attributes inferred automatically, like ` nogc`, `pure`, `nothrow`, ` safe`. Some people use them for that purpose. (Because of IFTI, functions with empty template parameters can be called with the same syntax as normal functions, they don't need the `!()`.)
That and it lets you use auto ref. It also can be necessary when overloading templated functions. It used to be that you couldn't overlooad a templated function with a non-templated function, forcing you to have functions with no template parameters if you wanted to have what would normally be a non-templated function overload a templated function. That's now been fixed, but the overload rules aren't quite the same between a non-templated functions and a templated function with no parameters, so sometimes you still need to templatize a function with empty parameters depending on what you're trying to do with your function overloads. - Jonathan M Davis
Jul 01 2015