Skip to content

Visual Studio: Require new preprocessor #46

@martinfinke

Description

@martinfinke

The conversion between enum and string doesn't seem to work using Visual Studio. The reason is that the preprocessor behaves incorrectly, so the string for an enum value becomes "WISE_ENUM_IMPL_IIF_1 (WISE_ENUM_IMPL_FIRST_ARG (SomeEnumValue, 0), (SomeEnumValue, 0))" instead of "SomeEnumValue".

This can be solved by enabling the new conforming preprocessor. wise_enum could check this at compile-time:

#ifdef _MSC_VER
#ifdef _MSVC_TRADITIONAL
#if _MSVC_TRADITIONAL
#error "The traditional, non-standard preprocessor is not supported. From Visual Studio 15.8 through 16.4, add the '/experimental:preprocessor' compiler option. From Visual Studio 16.5 onwards, add the '/Zc:preprocessor' compiler option."
#endif
#else
// _MSVC_TRADITIONAL is not defined, check from_string/to_string using a test enum:
namespace detail {
    enum class TestEnum { SomeTestValue };
}
static_assert(detail::strcmp(to_string(detail::TestEnum::SomeTestValue), "SomeTestValue") == 0
                  && from_string<detail::TestEnum>("SomeTestValue") == detail::TestEnum::SomeTestValue,
              "The traditional, non-standard preprocessor is not supported. From Visual Studio 15.8 through 16.4, add the '/experimental:preprocessor' compiler option. From Visual Studio 16.5 onwards, add the '/Zc:preprocessor' compiler option.");
#endif // _MSVC_TRADITIONAL
#endif // _MSC_VER

The static_assert is a fallback for Visual Studio < 15.8, where _MSVC_TRADITIONAL isn't defined at all.

I thought I'd share this, in case someone else runs into it. This info could also be added to the README.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions