1

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?

flamingo
  • 148
  • 1
  • 11

1 Answers1

1

You don't need to use decltype here, just use T:

template<class T>
std::ostream& operator<<(std::ostream& os, const T& foo)
{
    f< T >(os, foo);
    return os;
}

but when using decltype you will have to remove reference from foo type:

template<class T>
std::ostream& operator<<(std::ostream& os, const T& foo)
{
    f< std::remove_reference_t< decltype(foo) > >(os, foo);
    return os;
}

When template type deduction works T is deduced to be Foo [5] - what rank supports. But when you do decltype(foo), it returns type also with const & - so you have const Foo (&)[4]. rank for that thing just doesn't work.

rafix07
  • 20,001
  • 3
  • 20
  • 33