When I types the following as a stand-alone line:
std::endl;
I got the following error:
statement cannot resolve address for overloaded function
Why is that? Cannot I write std::endl; as a stand-alone line?
Thanks.
std::endl is a function template. Normally, it's used as an argument to the insertion operator <<. In that case, the operator<< of the stream in question will be defined as e.g. ostream& operator<< ( ostream& (*f)( ostream& ) ). The type of the argument of f is defined, so the compiler will then know the exact overload of the function.
It's comparable to this:
void f( int ){} void f( double ) {} void g( int ) {} template<typename T> void ft(T){} int main(){ f; // ambiguous g; // unambiguous ft; // function template of unknown type... } But you can resolve the ambiguity by some type hints:
void takes_f_int( void (*f)(int) ){} takes_f_int( f ); // will resolve to f(int) because of `takes_f_int` signature (void (*)(int)) f; // selects the right f explicitly (void (*)(int)) ft; // selects the right ft explicitly That's what happens normally with std::endl when supplied as an argument to operator <<: there is a definition of the function
typedef (ostream& (*f)( ostream& ) ostream_function; ostream& operator<<( ostream&, ostream_function ) And this will enable the compiler the choose the right overload of std::endl when supplied to e.g. std::cout << std::endl;.
Nice question!
The most likely reason I can think of is that it's declaration is:
ostream& endl ( ostream& os ); In other words, without being part of a << operation, there's no os that can be inferred. I'm pretty certain this is the case since the line:
std::endl (std::cout); compiles just fine.
My question to you is: why would you want to do this?
I know for a fact that 7; is a perfectly valid statement in C but you don't see that kind of rubbish polluting my code :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With