I'm trying to understand why a template partial specialization becomes invisible.
I'm doing a small example of how I reached to the error below.
The example tries to overload operator<<
to print to ostreams.
There is a solution that works in the question 1 for printing tuples. My question is about why the one below fails with the invisible error.
The full error from clang:
call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent
lookup
operator<<(os, std::get<0>(t));
^
testing.cpp:9:47: note: in instantiation of member function 'tuple_printer<1, std::__1::tuple<std::__1::tuple<const char *, int> > >::print'
requested here
tuple_printer<s-1, std::tuple<T...>>::print(os, t);
^
testing.cpp:33:52: note: in instantiation of member function 'tuple_printer<2, std::__1::tuple<const char *, int> >::print' requested here
tuple_printer<sizeof...(T), std::tuple<T...>>::print(os, t);
^
testing.cpp:40:15: note: in instantiation of function template specialization 'operator<<<const char *, int>' requested here
std::cout << std::make_tuple("hello", 5) << std::endl;
^
testing.cpp:30:15: note: 'operator<<' should be declared prior to the call site
std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& t)
The example code:
#include <tuple>
#include <iostream>
template<size_t s, typename... T>
struct tuple_printer{
static void print(std::ostream& os, const std::tuple<T...>& t){
os << ", ";
os << std::get<s-1>(t);
tuple_printer<s-1, std::tuple<T...>>::print(os, t);
}
};
template<typename... T>
struct tuple_printer<0, T...>{
static void print(std::ostream& os, const std::tuple<T...>& t){
//nothing to do here
}
};
template<typename... T>
struct tuple_printer<1, T...>{
static void print(std::ostream& os, const std::tuple<T...>& t){
//no need for comma separator
os << std::get<0>(t);
}
};
template <typename... T>
std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& t)
{
os << "[";
tuple_printer<sizeof...(T), std::tuple<T...>>::print(os, t);
return os << "]";
}
int main()
{
std::cout << std::make_tuple(2, 3.14159F, 2345.678) << std::endl;
std::cout << std::make_tuple("hello", 5) << std::endl;
std::cout << std::make_tuple() << std::endl;
return 0;
}