How to split the definition of template friend funtion within template class?

Posted by ~joke on Stack Overflow See other posts from Stack Overflow or by ~joke
Published on 2010-05-12T14:56:47Z Indexed on 2010/05/12 16:04 UTC
Read the original article Hit count: 143

Filed under:
|
|

The following example compiles fine but I can't figure out how to separate declaration and definition of operator<<() is this particular case.

Every time I try to split the definition friend is causing trouble and gcc complains the operator<<() definition must take exactly one argument.

#include <iostream>
template <typename T>
class Test {
    public:
        Test(const T& value) : value_(value) {}

        template <typename STREAM>
        friend STREAM& operator<<(STREAM& os, const Test<T>& rhs) {
            os << rhs.value_;
            return os;
        }
    private:
        T value_;
};

int main() {
    std::cout << Test<int>(5) << std::endl;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates