How does the left hand operand cout is passed here?
c++
Solution
The function `currency()` is a manipulator: The stream classes have special overloaded output operators taking functions with a specific sigunature as argument. They look something like this (with the templatization elided):
class std::ostream
public std::ios {
public:
// ...
std::ostream& operator<< (std::ios_base& (*manip)(std::ios_base&));
std::ostream& operator<< (std::ios& (*manip)(std::ios&));
std::ostream& operator<< (std::ostream& (*manip)(std::ostream&));
};
That is, `currency` is passed as a function pointer which gets called with the stream as its argument.
Problem
``` #include <iostream> #include <iomanip> using namespace std; ostream & currency(ostream & output) { output << "RS "; return output; } int main() { cout << currency << 7864.5; return 0; } ``` OUTPUT : ``` RS 7864.5 ``` I don't understand how this seems to work i.e just the name of the function `currency` is used to invoke the function. isn't this supposed to be like `currency(cout)` but using it gives output. ``` RS 1054DBCC7864.5 ```