core.reflect vs c++ reflection

Stefan Koch uplink.coder at googlemail.com
Sun Sep 26 19:03:24 UTC 2021


Good day,

It's hard to find good examples of the c++ reflection ts so I've 
copied one I've found in an earlier proposal
I am just putting the proposed c++ syntax here, next to how my 
core.reflect library would handle this case.


```C
#include <meta>
template<Enum T>
std::string to_string(T value) { // Could also be marked constexpr
     for constexpr (auto e : std::meta::members_of(reflexpr(T)) {
         if (exprid(e) == value) {
             return std::meta::name_of(e);
         }
     }
     return "<unnamed>";
}
```

```D
import core.reflect.reflect;
string toString(E)(E enumValue)
{
     static immutable ed = cast(EnumDeclaration)nodeFromName(E);
     foreach(m; ed.members)
     {
         if ((cast(IntegerLiteral)m.value).value == enumValue)
         {
             return m.name;
         }
     }
     return "unnamed";
}
```

Please let know what you think.


More information about the Digitalmars-d mailing list