Skip to content

LOGGING_SETTINGS_INITIALIZER not useable in C++ before C++20

The recommended way to initialize the logging settings is to use the macro LOGGING_SETTINGS_INITIALIZER. However, the way it is defined prevents its usage in C++ before C++20:

#define LOGGING_SETTINGS_INITIALIZER        \
    {                                       \
        .log_level = RIST_LOG_DISABLE,      \
        .log_cb = NULL,                     \
        .log_cb_arg = NULL,                 \
        .log_socket = -1,                   \
        .log_stream = NULL,                 \
    }

For a very unfortunate reason, C++ does not allow designated initializers (e.g. .log_level =) before C++20. Using this macro with C++ version below C++20 (the default is way below in all compilers) results in a compilation error.

Therefore, it would be recommended to either remove the designated initializers or condition their usage to C or C++20:

#if !defined(__cplusplus) || __cplusplus >= 202002L
#define LOGGING_SETTINGS_INITIALIZER        \
    {                                       \
        .log_level = RIST_LOG_DISABLE,      \
        .log_cb = NULL,                     \
        .log_cb_arg = NULL,                 \
        .log_socket = -1,                   \
        .log_stream = NULL,                 \
    }
#else
#define LOGGING_SETTINGS_INITIALIZER        \
    {                                       \
        RIST_LOG_DISABLE,                   \
        NULL,                               \
        NULL,                               \
        -1,                                 \
        NULL,                               \
    }
#endif

Assuming that the order of fields in the structure remains unchanged.