Overriding to!string on enum types

evilrat via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 2 06:30:18 PDT 2014


On Tuesday, 2 September 2014 at 12:54:55 UTC, Nordlöw wrote:
> Is it possible to override the behaviour of to!string(x) when x 
> is an enum. I'm asking because this
>
> enum CxxRefQualifier
> {
>     none,
>     normalRef,
>     rvalueRef
> }
>
> string toString(CxxRefQualifier refQ) @safe pure nothrow
> {
>     final switch (refQ)
>     {
>         case CxxRefQualifier.none: return "";
>         case CxxRefQualifier.normalRef: return "&";
>         case CxxRefQualifier.rvalueRef: return "&&";
>     }
> }
>
> doesn't affect behaviour of to!string(x) when x is an instance 
> of CxxRefQualifier.

i can think of two ways(both from memory)

1) specialized template override
template to(T: enum)(T s)
{
  string to(T s) { .. your custom code .. }
}

or just stringof
CxxRefQualifier crq = CxxRefQualifier.normalRef;
assert(crq == "normalRef");


More information about the Digitalmars-d-learn mailing list