Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 it?

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

Errors are:

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
like image 578
Scott Langham Avatar asked Sep 07 '17 19:09

Scott Langham


1 Answers

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

like image 92
Justin Avatar answered Oct 23 '22 20:10

Justin