Question about explicit template instantiation
Edward Diener
eddielee_no_spam_here at tropicsoft.com
Sun Feb 10 07:58:21 PST 2008
Tyro[a.c.edwards] wrote:
> Edward Diener さんは書きました:
>> I am working my way throught the pdf documentation ( 1.0 ) regarding
>> templates in D and in the topic about explicit template instantiation
>> and I came across the lines below, which have me stumped:
>>
>> ----------------------------------------------------------------
>>
>> A template instantiation can be aliased:
>> template TFoo(T) { alias T* t; }
>> alias TFoo!(int) abc;
>> abc.t x; // declare x to be of type int*
>> Multiple instantiations of a TemplateDeclaration with the same
>> TemplateArgumentList, before
>> implicit conversions, all will refer to the same instantiation. For
>> example:
>> template TFoo(T) { T f; }
>> alias TFoo!(int) a;
>> alias TFoo!(int) b;
>> ...
>>
>> a.f = 3;
>> assert(b.f == 3); // a and b refer to the same instance of TFoo
>> This is true even if the TemplateInstances are done in different modules.
>>
>> ----------------------------------------------------------------
>>
>> My confusion with the above is that an alias, when referring to a
>> type, is the equivalent of a C++ typedef AFAICS. So 'a' and 'b' above
>> are types ( because the instantiated template is a type ) and then I
>> see the 'a.f=3' line followed by the 'assert(b.f == 3)' line and I can
>> not understand how a type can be used in either expression.
>>
>> I am also assuming that the line beginning with "Multiple
>> instantiations of a..." only mean that the 'a' and 'b' refer to the
>> same type, but perhaps I have missed something in this explanation.
>
> The problem here is that you are misconstruing templates to be classes.
> Templates, like classes and other types, can be aliased, however since a
> template is not a type you couldn't use TFoo!(int) to declare a
> variable. This also applies to the derived aliases 'a' and 'b'.
I understand what a template is and that the instantiation of a template
is a type ( class ). So in the example above 'a' and 'b' are aliases for
types, ie. typedef in C++. Therefore the statements "a.f = 3;" and
'assert(b.f == 3);" made no sense to me.
But I think I understand them now, but hopefully someone will clarify
things for me. It seems in D one can instantiate a temporary object from
a type which has a default constructor simply by using the type, whereas
in C++ one must use the type followed by '()'. So whereas if X were a
class with a default constructor with the data member Y, in C++ one
would use 'X().Y' to access the temporary's Y data member while in D it
appears one can use 'X.Y' to access the temporary's data member.
The previous explanation is the only way I can make sense of the two
statements above which led to my OP.
>
> The templated class however, is a different story. Take the following
> code snipet for example:
>
> class Bar(T)
> {
> T f;
> }
>
> alias bar!(int) y;
> alias bar!(int) z;
>
> Here Bar(T) is a class, therefore Bar!(int) is a class instanciation: a
> type. Hence, the aliases, 'y' and 'z' are also types. Any attempts to
> use them as with the previous example, will result in an error.
>
> y.f = 3; // Error y is an instanciated class
> assert(z.f == 3); // Error y is an instanciated class
What I quoted in my OP was all directly from the documentation, not
anything I made up. Somehow you think that the lines:
a.f = 3;
assert(b.f == 3); // a and b refer to the same instance of TFoo
were inserted by me. No ! They were in the doc and they appeared to be
incorrect, and I wanted an explanation for them.
More information about the Digitalmars-d
mailing list