Template questions

Jason House jason.james.house at gmail.com
Sat Mar 22 17:56:08 PDT 2008


Edward Diener wrote:

> I am going to be asking a number of questions about D templates in
> various posts, and I hope others can help me understand D templates
> despite the documentation.
> 
> First question:
> 
> In the documentation on templates I read these lines:
> 
> "Even if template arguments are implicitly converted to the same
> template parameter type, they still refer to different instances:
> struct TFoo(int x) { }
> static assert(is(TFoo!(3) == TFoo!(2 + 1))); // 3 and 2+1 are both 3 of
> // type
> int
> static assert(!is(TFoo!(3) == TFoo!(3u))); // 3u and 3 are different
> // types"
> 
> To what is the "int" on line 6 refer ?
> Why is line 3 considered a D template ? It does not begin with 'template
> ...'. It looks like a simple 'struct' to me with a public member. How
> then can it be instantiated as if it were a template ?
> 
> Otherwise I do understand what is being said about templates being
> instantiated to different instances.

You'll help people out if you give us the URL of the page or file you're
looking at, for example http://www.digitalmars.com/d/2.0/template.html

How are you extracting line numbers?

Here's text I found online (reformatted to avoid word wrap issues)

struct TFoo(int x) { }
// 3 and 2+1 are both 3 of type int
static assert(is(TFoo!(3) == TFoo!(2 + 1)));
// 3u and 3 are different types
static assert(!is(TFoo!(3) == TFoo!(3u)));

Sadly, I can't help because this doesn't make sense to me either.  The
template parameter is of type int, so I'd expect 3 and 3u to be converted
to an int (3) to fit the specified type... and therefore be of the same
type.



More information about the Digitalmars-d mailing list