为什么这个enable_if函数模板不能专门用于VS2017?

时间:2017-09-07 19:46:30

标签: c++ visual-studio-2017

以下使用VS2015编译,但在VS2017中失败并出现以下错误。代码是否采用了VS2017中已修复的非标准内容,还是VS2017应该编译它?

#include "stdafx.h"
#include <type_traits>

template <typename E>
constexpr auto ToUnderlying(E e)
{
    return static_cast<std::underlying_type_t<E>>(e);
}

template<typename T>
bool constexpr IsFlags(T) { return false; }

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

enum class PlantFlags { green = 1, edible = 2, aromatic = 4, frostTolerant = 8, thirsty = 16, growsInSand = 32 };

bool constexpr IsFlags(PlantFlags) { return true; }

int main()
{
    auto ored = PlantFlags::green | PlantFlags::frostTolerant;

    return 0;
}

错误是:

c:\main.cpp(24): error C2893: Failed to specialize function template 'enable_if<false,_Ty>::type
operator |(E,E)'
        with
        [
            _Ty=underlying_type<_Ty>::type
        ] 
c:\main.cpp(24): note: With the following template arguments:
c:\main.cpp(24): note: 'E=PlantFlags'
c:\main.cpp(24): error C2676: binary '|': 'PlantFlags' does not define this operator or a conversion to a type acceptable to the predefined operator

2 个答案:

答案 0 :(得分:4)

这可能是Visual Studio中的错误。可能的解决方法可能是使用模板专门化而不是重载:

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);
}

答案 1 :(得分:4)

这绝对是Visual Studio中的一个错误。它compiles on GCC and Clang。它似乎与作为模板参数计算的constexpr函数有关。作为临时解决方法,您可以创建模板变量:

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