I wonder what is the difference between this code that works :
#include <type_traits>
#include <iostream>
template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>;
template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>;
template<typename T, is_ref<T> = true>
void foo(T&&) {
std::cout << "ref" << std::endl;
}
template<typename T, is_not_ref<T> = true>
void foo(T&&) {
std::cout << "not ref" << std::endl;
}
int main() {
int a = 0;
foo(a);
foo(5);
}
and this one that does not work :
#include <type_traits>
#include <iostream>
template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>;
template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>;
template<typename T, typename = is_ref<T>>
void foo(T&&) {
std::cout << "ref" << std::endl;
}
template<typename T, typename = is_not_ref<T>>
void foo(T&&) {
std::cout << "not ref" << std::endl;
}
int main() {
int a = 0;
foo(a);
foo(5);
}
What is the true difference between typename = is_ref<T>
and is_ref<T> = true
?
typename =
in all cases when it works, and the other one when you need to. Or use theenable_if<bool> =
everytime – Egidius