When testing SFINAE, I found something that I think should not work

I found an interesting conditional function exception that I got from this site , and when testing, I came across this:

#include<type_traits>

namespace detail
{
    enum enabler {};
}

template <int overload, typename Condition>
using EnableIf = std::enable_if_t<Condition::value, detail::enabler>;

template <typename T,
    EnableIf<0, std::is_same<T, int>>...>
    T twice(T t) { return 2 * t; }

template <typename T,
    EnableIf<0, std::is_same<T, float>>...>
    T twice(T t) { return 2 * t; }

int main()
{
    twice(1);
    twice(1.f);
    return 0;
}

Should this not cause a compiler error, because the type is the EnableIfsame for both functions? I was going to use a different number for each overload and have a template class that would contain an enumler enum, so that would be a different type, but it doesn't seem to be necessary. Is this a defect or am I missing something?

I tested this on VC ++ (2017) and clang .

Although the VC ++ compiler does not complain, he obviously does not like intellisense:

enter image description here

, , , , , , .

+1
2

, . , , , , .

, . , . , , , .

, T, .

, EnableIf, , , , 2- detail::enabler, ( , , ). , .

, @Barry , std::enable_if_t<std::is_same<T, int>::value, detail::enabler> std::enable_if_t<std::is_same<T, float>::value, detail::enabler> . . , , , , , .

0

, EnableIf ?

. std::enable_if_t<std::is_same<T, int>::value, detail::enabler>, std::enable_if_t<std::is_same<T, float>::value, detail::enabler>. , . , , - .

. , - , , ​​ .

EnableIf , (detail::enabler).

+3

Source: https://habr.com/ru/post/1672361/


All Articles