Concepts like c++20 with specialized overload resolution.

ryuukk_ ryuukk.dev at gmail.com
Sat May 27 16:10:49 UTC 2023


On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote:

you can use: ``static if (__traits(hasMember, T, "magma"))``


```D
import std;

struct LavaMan {
   void magma() { writeln(" LavaMan is throwing LAVA"); }
}

struct FakeVulcano {
   void try_making_lava() { writeln( " Making fake lava"); }
}


// all the magic
void make_lava(T)(ref T lava_thing){
     static if (__traits(hasMember, T, "magma"))
         lava_thing.magma();
     else
     	lava_thing.try_making_lava();
}

int main() {
   LavaMan v;
   FakeVulcano l;
   make_lava(l);
   make_lava(v);
   return 0;
}
```

```
  Making fake lava
  LavaMan is throwing LAVA
```


More information about the Digitalmars-d-learn mailing list