lundi 23 mai 2022

How to define the different template structs with different enums

I've two enums as below:

enum TTT {t = 2};

enum XXX {x = 2};

I'm trying to make some struct to help me get the name of an enum. Here is what I've done:

template<TTT>
struct StrMyEnum {
    static char const* name() { return "unknown"; }
};

template<>
struct StrMyEnum<t> {
    static char const* name() { return "tt"; }
};

It works. Now I can get the name of the enum t: StrMyEnum<t>::name().

However, if I write another group of template structs for XXX, it doesn't seem to work.

template<XXX>
struct StrMyEnum {
    static char const* name() { return "unknown"; }
};

template<>
struct StrMyEnum<x> {
    static char const* name() { return "xx"; }
};

Now I get a compile error: could not convert 'x' from 'XXX' to 'TTT'. It seems that the compiler is trying to match the StrMyEnum<x> with TTT...

I don't know why.

So template can't distinguish the different enums? Is it possible to write some structs for different enums?

Aucun commentaire:

Enregistrer un commentaire