template parameters
Ali Çehreli
acehreli at yahoo.com
Sun Aug 29 21:41:02 UTC 2021
On 8/29/21 11:32 AM, Charles H. wrote:
> I've set up a class template (so far untested) thus:
>
> class AARL (T, ndx = "ndx")
> if (isIntegral(T.init.ndx) )
>
> If I'm correct, this should work for ndx an integer variable of T,
> but I'd really like T to be able to be anything which can be stored both
> in an array and in an associative array. But I can't figure out how to
> specify ndx. After all, T might be a float, so it wouldn't have any
> appropriate attributes. And I'd also like ndx to be able to be a
> function either a member of the class/struct T or a stand alone function.
>
> So how should I set up this template?
>
I came up with the following:
template supportsCall(T, string func) {
import std.format : format;
import std.traits : isIntegral;
enum expr = format!q{
enum supportsCall = isIntegral!(typeof(T.init.%s()));
}(func);
mixin (expr);
}
class AARL (T, string func = "ndx")
if (supportsCall!(T, func)) {
}
int ndx(int i) {
return 42;
}
struct S {
long ndx() {
return 100;
}
}
void main() {
auto a = new AARL!int();
auto b = new AARL!S();
}
Ali
More information about the Digitalmars-d-learn
mailing list