Why can't this enable_if function template be specialized in VS2017?

后端 未结 2 718
无人及你
无人及你 2021-02-09 10:38

The following compiled with VS2015, but fails in VS2017 with the below errors. Was the code doing something non standard that has been fixed in VS2017, or should VS2017 compile

相关标签:
2条回答
  • 2021-02-09 11:07

    This is definitely a bug in Visual Studio. It compiles on GCC and Clang. It seems to be related with constexpr functions evaluated as template parameters. As a temporary workaround, you can make a template variable:

    template <typename T>
    bool constexpr is_flags_v = IsFlags(T{});
    
    template<typename E>
    std::enable_if_t<is_flags_v<E>, std::underlying_type_t<E>> operator | (E lhs, E rhs)
    {
        return ToUnderlying(lhs) | ToUnderlying(rhs);
    }
    

    On Godbolt

    0 讨论(0)
  • 2021-02-09 11:27

    This might be a bug in Visual Studio. A possible workaround could be using template specialization instead of overloading:

    template <typename T>
    struct is_flags { constexpr static bool value = false; };
    
    template <>
    struct is_flags<PlantFlags> { constexpr static bool value = true; };
    
    template<typename E> std::enable_if_t<is_flags<E>::value, std::underlying_type_t<E >> operator | (E lhs, E rhs)
    {
        return ToUnderlying(lhs) | ToUnderlying(rhs);
    }
    
    0 讨论(0)
提交回复
热议问题