I have a number of operator<<() functions that start by doing something similar, so I want to abstract that. Here's a minimal reproducible case of what I'm trying to do (with all the complexity ripped out). Note that it doesn't compile. If it did compile, I would expect the program to print the number 3 on a line by itself.
/* clang++ -std=c++14 -Wall -Wextra foo.cc -o foo */ #include <ostream> #include <iostream> using std::cout; using std::endl; using std::ostream; ostream& BaseFunction(ostream& os, const int x) { return os << x; } int main(int argc, char *argv[]) { cout << BaseFunction(cout, 3) << endl; } The error starts like this:
foo.cc:17:8: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'ostream') cout << BaseFunction(cout, 3) << endl; ~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~ and then offers a whole lot of "no known conversion from ostream to T" suggestions.
Anyone see what I've done wrong?
coutinBaseFunction(cout, 3) << endl;and it should work.