More specific instantiations
bearophile
bearophileHUGS at lycos.com
Mon Feb 13 21:08:14 PST 2012
> @templated(T) means that foo2() is shared across the various instantiations of Bar that share the same U and W types, so it's a template of T only. This is useful as one tool to fight template bloat, and it has other purposes too (like avoiding some template instantiation errors, because you are asserting that foo2 does not need the types U and W to be fully defined correctly).
I missed another potential usage.
This is a common D idiom, present in Phobos too:
struct Foo(T) {
T xx;
}
Foo!T foo(T)(T x) {
return Foo!T(x);
}
void main() {
auto f = foo(5);
}
Maybe is replaceable with this, that avoids the need of a separate global function, I don't know if this is meaningful:
struct Foo(T) {
T xx;
@templated() static Foo!U opCall(U)(U x) {
return Foo!U(x);
}
}
void main() {
auto f = Foo(5);
}
But Foo can't have a normal ctor, I think.
Bye,
bearophile
More information about the Digitalmars-d
mailing list