Templates class member functions not conditional?
bearophile
bearophileHUGS at lycos.com
Tue Sep 11 04:33:33 PDT 2012
monarch_dodra:
> Is this not the case for D? Or is it currently a limitation?
In this case I think D is working as designed. All functions
inside a template are created when you instantiate it.
> Can I ever expect we'll get a "conditionally compiled on
> requirement" functionality for template struct member functions.
I have asked for a @templated(Arg1, Arg2, ...) that's usable for
this purpose too, but Walter has not commented so far.
> struct C(T)
> {
> private T val;
> static if(isAssignable(T!T))
> {
> @property void front(T value)
> {val = value;}
> }
> }
Probably such use of a static if (or debug, version, etc) is the
idiomatic way to do what you want in D. It's easy to see and
understand for the person that reads the code.
> struct C(T)
> {
> private T val;
> @property void front()(T value)
> {val = value;}
> }
>
> This works, and is correctly "conditionally compiled on
> requirement". The signature is kind of kludgy, but it works...
> AND, if someone *does* attempt to make the call, then a verbose
> compile error appears.
This doesn't look bad, just remember this doesn't work:
struct C(T) {
private T val;
@property void front()(T value) {
val = value;
}
}
void main() {
C!int ci;
auto f = &ci.front;
assert(ci.val == 0);
f(1);
assert(ci.val == 1);
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list