Templated Enums?

Johannes Pfau nospam at example.com
Wed Jun 6 12:27:24 PDT 2012


Am Wed, 06 Jun 2012 21:01:21 +0200
schrieb "Era Scarecrow" <rtcvb32 at yahoo.com>:

>   I've come across an interesting problem. You can template quite 
> a few items, but not enums? Some thoughts from the experts would 
> likely be nice. Consider...
> 
> enum(T) X{ //syntax error
>      bitsPerT = T.sizeof * 8,
>      //further calculation types can follow
> }
> 
> assert(X!(byte).bitsPerT == 8);
> 
> The compile complains outright you can't do this, but enclosing 
> it in a struct makes that problem go away with the same results.
> 
> struct(T) X {
>      enum {
>          bitsPerT = T.sizeof * 8,
>      }
> }
> 
> assert(X!(byte).bitsPerT == 8); //compiles and works
> 

According to http://dlang.org/template.html
something like this:
---------------
class Bar(T) { T member; }
---------------

is actually only syntactic sugar for this:
---------------
template Bar(T) { class Bar { T member; } }
---------------

So instead of
---------------
enum(T) X
{
     bitsPerT = T.sizeof * 8,
}
---------------

it should be possible to do this:
---------------
template X(T)
{
    enum X
    {
        bitsPerT = T.sizeof * 8,
    }
}
---------------


More information about the Digitalmars-d-learn mailing list