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'
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With