My code is
template<typename T, typename = void>
struct is_container : std::false_type {};
template<typename T>
struct is_container <T, std::void_t<decltype(std::begin(std::declval<T>())), decltype(std::end(std::declval<T>()))>> : std::true_type {};
template<typename T, std::size_t N>
struct is_container<T[N]> :std::true_type {};
// fun1
template<typename T>
typename std::enable_if<is_container<T>::value>::type printElement(const T& v) {
for (const auto& i : v) {
printElement(i);
}
std::cout << 'n';
}
// fun2
template<typename T>
typename std::enable_if<!is_container<T>::value>::type printElement(const T& v) {
std::cout << v << ' '<<'t';
}
When I use it to output a nested vector, it can’t compile and error message is:
C2672 “printElement”:No matching overloaded function found
Interestingly, if I swap the position of fun1 and fun2, it works! But why?
New contributor
softdream is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.