I was trying to understand how SFINAE
works and I was experimenting with this code
#include <type_traits>
struct One {
using x = int;
};
struct Two {
using y = int;
};
template <typename T, std::void_t<typename T::x>* = nullptr>
void func() {}
template <typename T, std::void_t<typename T::y>* = nullptr>
void func() {}
/*template <typename T, std::enable_if_t<std::is_same_v<typename T::x, typename T::x>>* = nullptr>
void func() {}
template <typename T, std::enable_if_t<std::is_same_v<typename T::y, typename T::y>>* = nullptr>
void func() {} */
int main() {
func<One>();
func<Two>();
}
The commented code works but the first doesn't. The compiler gives me errors saying that there is a redefinition and that template argument deduction failed. Could someone explain why this happens? The two void_t
s should be independent right? Since one line checks for x
and the other for y
. How can I fix?
make_void
trick. – Teofilateosinte