I want to overload the operator << for streams so that when I call it with a function that takes a stream it will call the function (to optimize string concatenation). However, it doesn't seem to be working for member functions.
#include <iostream>
std::ostream& operator<< (std::ostream& s, void(*f)(std::ostream&)) {
f(s);
return s;
}
void hello (std::ostream& s) {
s << "Hello, World!";
}
class Foo {
public:
void hello (std::ostream& s) {
s << "Hello from class.";
}
};
int main(int argc, char const *argv[])
{
// This works
std::cout << hello << std::endl;
// This doesn't
Foo o;
std::cout << o.hello << std::endl;
return 0;
}
This code fails to compile with the error "error: invalid use of non-static member function ‘void Foo::hello(std::ostream&)’". After some searching, I discovered that I needed a function which takes a pointer to member in the form void(Foo::*)()
, however adding the following to the top of the code did not get the code to compile.
template <typename T>
std::ostream& operator<< (std::ostream& s, void(T::*f)(std::ostream&)) {
f(s);
return s;
}
Is there any way to make this work?