vendredi 25 septembre 2015

Replace macro with constexpr function to define enum

I have a macro such as

#define lower_ascii_to_3bit(c) c & 0x60 - 1

I have tried to replace it with this:

constexpr const std::uint8_t lower_ascii_to_3bit(char c)
{
    return c & 0x60 - 1;
}

I am using it like this:

enum class Column
{
    A = ascii_to_3bit('a'),
    B = ascii_to_3bit('b'),
    C = ascii_to_3bit('c'),
    D = ascii_to_3bit('d'),
    E = ascii_to_3bit('e'),
    F = ascii_to_3bit('f'),
    G = ascii_to_3bit('g'),
    H = ascii_to_3bit('h')
};

It works fine as a macro but dies with "function call must have a constant value with a constant expression" when it is a function.

Is it possible?

Aucun commentaire:

Enregistrer un commentaire