Search code examples
c++templatescoutnested-classtype-deduction

std::ostream& operator<< type deduction


I have a template function like this one

#include <list>
#include <iostream>

template<typename T>
std::ostream& operator<<(std::ostream& out, const std::list<T>& list){
    out << "[";
    if(!list.empty()){
        typename std::list<T>::const_iterator it = list.cbegin();
        out << *it;
        for (++it; it != list.cend(); ++it){
            out << ", ";
            out << *it;
        }
    }
    out << "]";
    return out;
}

And some template class with nested classes

namespace my{

    template<
        typename T,
        typename U = size_t
    >

    class graph{

    public:
        typedef T dist_t;
        typedef U node_t;

        class node_pt;
        typedef struct arc_t{
            node_pt* from = nullptr;
            node_pt* to = nullptr;
            dist_t weight;
        } arc_t;
        typedef struct arc_pt{
            arc_t arc;
        } arc_pt;
        typedef struct node_pt{
            node_t node;
        } node_pt;

        class arc_iterator{
        public:
            arc_pt* pt = nullptr;
        public:
            arc_pt* operator->() const{
                return pt;
            }

            friend std::ostream& operator<< (std::ostream &out, const arc_iterator& it) {
                out << "(" << it->arc.from->node << "," << it->arc.to->node << "," << it->arc.weight << ")";
                return out;
            }
        };

        class node_iterator{
        public:
            node_pt* pt = nullptr;

        public:

            node_t operator *() const{
                return pt->node;
            }

            friend std::ostream& operator<< (std::ostream &out, const node_iterator& it) {
                out << *it;
                return out;
            }
        };

    };
}

Some code to reproduce the problem

namespace my{
    namespace test{
        void run(){     
            typedef my::graph<size_t> graph_t;
            std::list<graph_t::node_t> l1;
            std::list<graph_t::dist_t> l2;
            std::list<graph_t::node_iterator> l3;
            std::list<graph_t::arc_iterator> l4;

            std::cout << l1 << std::endl;
            std::cout << l2 << std::endl;
            std::cout << l3 << std::endl;
            std::cout << l4 << std::endl;
        }
    }
}


int main(){
    my::test::run();
}

The problem is it doesn't compile if I define the two friend methods. If I only define one method and comment one of the iterator list printing it works.

The error I'm getting is

src/OTest_Graph.cpp: In member function ‘virtual void my::test::TestGraph::run()’:
src/OTest_Graph.cpp:59:53: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
In file included from /usr/include/c++/4.7/iostream:40:0,
                 from h/OTest_Graph.h:4,
                 from src/OTest_Graph.cpp:1:
/usr/include/c++/4.7/ostream:600:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::list<my::graph<long unsigned int>::node_iterator, std::allocator<my::graph<long unsigned int>::node_iterator> >]’

Can anyone tell me what's going on here?


Solution

  • The error depends on the version of the standard library, see @matt-whitlock's answer.

    A solution for g++ 4.7 :

    Instead of

    std::cout << l1 << std::endl;
    std::cout << l2 << std::endl;
    std::cout << l3 << std::endl;
    std::cout << l4 << std::endl;
    

    use

    ::operator<<(std::cout, l1) << std::endl;
    ::operator<<(std::cout, l2) << std::endl;
    ::operator<<(std::cout, l3) << std::endl;
    ::operator<<(std::cout, l4) << std::endl;