samedi 28 mars 2020

enum overloading operator | constexpr and runtime

I have a predeclared enum:

enum class TheEnum : std::uint32_t;

Followed by operator overloading:

constexpr TheEnum operator|(TheEnum const a, TheEnum const b)
{
    return TheEnum(std::uint32_t(a) | std::uint32_t(b));
}

With the definition:

enum class TheEnum : std::uint32_t
{
    A = 1 << 0,
    B = 1 << 1,
    C = 1 << 2,
    D = 1 << 3,
    Val0 = TheEnum::A | TheEnum::C,
    Val1 = TheEnum::A | TheEnum::D,
    Val2 = TheEnum::B | TheEnum::D
};

I try to have the operator| constexpr and not constexpr in the same time. Currently if I add the (not constexpr):

inline TheEnum operator|(TheEnum const a, TheEnum const b)
{
    return TheEnum(std::uint32_t(a) | std::uint32_t(b));
}

My compiler said it's already defined. The constexpr allow me to do some typetrait operations, but I need the same operators for runtime evaluation.

Aucun commentaire:

Enregistrer un commentaire