Forcing usage of one alias of an enum c++

130 views Asked by At

I want to give an enum multiple aliases, but force the calling code to use one specific alias when passing an enum value to a specific class. Here's my bright idea, which doesn't work:

template<typename Stype>
struct MoreDifferentS : Stype {};

struct S {
    enum consumate {
        v,
        vv
    };
};

class Dragon {
    void Burn(S::consumate type) {};
public:
    using mdS = MoreDifferentS<S>;
    void Burn(mdS::consumate type) {};
};

//Usage:
Dragon d;
d.Burn(S::v); //I don't want this to compile
d.Burn(mdS::v); //I want this to compile

The problem with this, is that the compiler still sees both functions as taking the same argument type, and throwing either "member function already defined or declared", or throwing "function Burn(S::consumate) inaccessible" when I try to call Burn(mdS::v). Is there some around this, or am I barking up entirely the wrong tree?

Thanks

1

There are 1 answers

0
lequinne On BEST ANSWER

n.m. suggested forcing S to instantiate as a template. This code accomplishes exactly what I want:

template<int>
struct S_Base {
    enum consumate {
        v,
        vv
    };
};

using S = S_Base<1>;
using mdS = S_Base<2>;

class Dragon {;
    void Burn(S::consumate type) {};
public:
    void Burn(mdS::consumate type) {};
};

//Usage:
Burn(S::v); //Doesn't compile
Burn(mdS::v); //Does compile

Two different derived classes of S do not do not produce a new type for S and its members, but two different template instantiations do.