I am trying to introduce the overloading mechanism based on rank of the object. I managed to implement simple example based on other post and it works for simple types: https://coliru.stacked-crooked.com/a/8129de0ae8a71af1
Now I would like to do sth similar for custom type:
#include <iostream>
#include <type_traits>
#include <sstream>
class Foo {
};
template < class T,
typename std::enable_if <(std::rank<T>::value == 0), int>::type = 0 >
void f(std::ostream& os, const T& value)
{
os << "rank: 0" << std::endl;
}
template< class T,
typename std::enable_if<(std::rank<T>::value == 1), int>::type = 0 >
void f(std::ostream& os, const T& value)
{
os << "rank: 1" << std::endl;
}
template <class T>
std::ostream& operator<<(std::ostream& os, const T& foo)
{
f<decltype(foo)>(os, foo);
return os;
}
int main()
{
Foo foo0;
Foo foo1[5];
//std::cout << std::rank<decltype(foo0)>{} << "\n"; // 0
//std::cout << std::rank<decltype(foo1)>{} << "\n"; // 1
// correct
f<decltype(foo0)>(std::cout, foo0); //prints: rank: 0
f<decltype(foo1)>(std::cout, foo1); //rank: 1
// invalid
std::cout << foo0; //prints: rank: 0
std::cout << foo1; //rank: 0
return 0;
}
https://coliru.stacked-crooked.com/view?id=cf91cec14a111f70
When I am calling functions directly, I am receiving valid results, but when I am doing it by << each time I am getting 0. Could you explain what I am doing wrong?