Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deduction failure when using a constexpr function?

So I was doing further testing based on this question, and I'm still a little unclear about how type deduction works.

If I use the following:

template<typename T, std::enable_if_t<std::is_same<T,int>::value, int> = 0>
inline auto fnx(T) -> int
{
    return 0;
}

template<typename T, std::enable_if_t<std::is_same<T, float>::value, int> = 0>
inline auto fnx(T) -> int
{
    return 0;
}

inline void fn()
{
    fnx(1);
    fnx(1.f);
}

I get no compilation errors. But when I do this:

template <bool TRUTH>
constexpr bool value() { return TRUTH; }

template<typename T, std::enable_if_t<value<std::is_same<T,int>::value>(), int> = 0>
inline auto fnx(T) -> int
{
    return 0;
}

template<typename T, std::enable_if_t<value<std::is_same<T, float>::value>(), int> = 0>
inline auto fnx(T) -> int
{
    return 0;
}

inline void fn()
{
    fnx(1);
    fnx(1.f);
}

The type deduction fails. What is happening here? Why is going through a constexpr function making it not valid? Or is this a problem with my c++ compiler? I'm using VC++2015.

The errors are:

error C2672: 'detail::fnx': no matching overloaded function found
error C2783: 'int detail::fnx(T)': could not deduce template argument for '__formal'
note: see declaration of 'detail::fnx'
like image 694
Adrian Avatar asked Mar 15 '17 13:03

Adrian


2 Answers

Your code is fine and completely valid. You must however not use constexpr function to do SFINAE with visual studio 2015. You can read more about expression SFINAE in MSVC here: Expression SFINAE improvements in VS 2017 RC

Upgrading to 2017 will solve your issue.

like image 150
Guillaume Racicot Avatar answered Oct 18 '22 18:10

Guillaume Racicot


This is a bug in your version of Visual Studio. Try upgrading to 2017.

I could not reproduce with 19.00.23506

Note that gcc 7.0.1 and Clang 5.0.0 had no issues.

like image 2
AndyG Avatar answered Oct 18 '22 18:10

AndyG