4

I'm working hard in order to translate many macros into (scoped, type-safe) const values. My goal is to use them, if necessary, with if constexpr.

For now I've managed to have satisfactory results with some macros using stringify macros and template functions:

#define STRINGIFY(X) #X
#define TO_STRING(X) STRINGIFY(X)

The macros above have a surprising different behaviour depending on the given parameter:

std::cout << TO_STRING(_DEBUG) << '\n';

Shows _DEBUG if (and only if) the macro _DEBUG is NOT defined, but if defined it shows the macro value (or an empty string if defined but without value).

Anyway, the macro result would always be a text literal so I'm using a constexpr function to check the result:

template <int SIZE>
constexpr bool b(const char (&definition)[SIZE])
{
    return definition[0] != '_';
}

And now combining STRINGIFY and b, it is possible to create enumerations and use them in if constexpr (instead of using #ifdef chains):

enum operating_system : bool
{
    iOS     = b(TO_STRING(__APPLE__)),
    Windows = b(TO_STRING(__MINGW32__)),
    Linux   = b(TO_STRING(__linux__)),
};

int main()
{
    if constexpr (operating_system::Windows)
    {
        // Specific Windows stuff.
    }
    else if constexpr (operating_system::iOS)
    {
        // Specific iOS stuff.
    }

    // Platform-independent sutff.

    return 0;
}

I'm not happy using a helper function to translate the literals to bool values (b function) but isn't a big deal. The real problem is that it relies on detecting the starting underscore (_) in order to detect the non-existent macros. So an existing macro with a value starting with underscore would be a false positive. Also, the real value of the macro is lost, let's see an example:

#define _DEBUG 0
#define DRIVERS _09072007

template <int SIZE>
constexpr int i(const char (&definition)[SIZE])
{
    return definition[0] != '_'; // what shall I put here?...
}

enum stuff : int
{
    cpp_version     = i(TO_STRING(__cplusplus)),
    debug_enabled   = i(TO_STRING(_DEBUG)),
    drivers_version = i(TO_STRING(DRIVERS)),
};

int main()
{
    std::cout << "C++ version: "     << stuff::cpp_version << '\n'
              << "Debug mode: "      << stuff::debug_enabled << '\n'
              << "Drivers version: " << stuff::drivers_version << '\n';

    return 0;
}

The code above shows:

C++ version: 1
Debug mode: 1
Divers version: 0

Expected, but not satisfactory. I need a way to translate those string literals to the real value (at compile time) and a workaround of the naive approach of the "it exists if not starts with underscore" problem that the DRIVERS macro is showing (maybe ignoring non numeric values until a numeric value is found?).

I've tried with recursive approach but indexing a string literal is not a constant expression:

constexpr int power10(int n)
{
    if (n == 0)
        return 1;

    return 10 * power10(n - 1);
}

template <int SIZE>
constexpr int v(const char (&definition)[SIZE], int INDEX)
{
    // error: 'definition' is not a constant expression
    constexpr char c = definition[INDEX];


    if (INDEX >= 0)
    {
        if constexpr (c >= '0' && c <= '9')
        {
            return v(definition, INDEX - 1) + (power10(SIZE - INDEX - 2) * (c - '0'));
        }
        else
        {
            return 0 + v(definition, INDEX - 1);
        }
    }

    return 0;
}

template <int SIZE>
constexpr int f(const char (&definition)[SIZE])
{
    return v(definition, SIZE - 2);
}

enum operating_system : bool
{
    // error: enumerator value for 'iOS' is not an integer constant
    iOS     = f(TO_STRING(__APPLE__)),
    // error: enumerator value for 'Windows' is not an integer constant
    Windows = f(TO_STRING(__MINGW32__)),
    // error: enumerator value for 'Linux' is not an integer constant
    Linux   = f(TO_STRING(__linux__)),
};
10
  • What about #if and #ifdef directives, without gratuitous strings? Feb 28, 2017 at 12:02
  • I want to avoid using macros as much as I can. And I'm not the only one. Feb 28, 2017 at 12:07
  • 1
    @llonesmiz well I'm not sure what is the OP expected result but pointing that he can easily use for loop inside the constexpr function and the result is still constexpr (as long as c++14 is available, and as if constexpr is in a game I assume it is)
    – W.F.
    Feb 28, 2017 at 12:28
  • 1
    @W.F. it Works with a for loop like a charm! Thank you! Would you be so kind to write an answer in order to accept it? Feb 28, 2017 at 14:34
  • 1
    @PaperBirdMaster As you observed STRINGIFY(MACRO) is "MACRO" only when MACRO is undefined but #MACRO is always "MACRO". You can possibly use that to decide if a macro is defined or not. This approach is probably not suitable for you (introduces Boost dependencies and complexity) but it shows that it is possible to do what you want.
    – llonesmiz
    Feb 28, 2017 at 15:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.