template parameters

Charles Hixson charleshixsn at earthlink.net
Sun Aug 29 22:31:04 UTC 2021


Thanks.  I going to have to study:

enum supportsCall = isIntegral!(typeof(T.init.%s()));


for awhile to make any sense of that, but it looks like just what I was 
looking for.


On 8/29/21 2:41 PM, Ali Çehreli via Digitalmars-d-learn wrote:
> 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
>
-- 
Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.



More information about the Digitalmars-d-learn mailing list