L
Lionel B
In an overloaded output operator for a class I want to detect ostream manipulators such as flush, endl, ends, etc. I am
stymied by how to do this; here's a minimal program indicating my problem:
// test.cpp
#include <iostream>
void foo(std:stream& (*f)(std:stream&))
{
if (f == &std::endl) std::cout << "Detected 'endl'\n"; // line 5
}
int main()
{
foo(std::endl);
return 0;
}
// test.cpp
g++ (3.4.1) barfs on this with:
g++ -W -Wall test.cpp -o test.exe
test.cpp: In function `void foo(std:stream&(*)(std:stream&))':
test.cpp:5: error: address of overloaded function with no contextual type information
at the indicated line. I don't quite see what more information the compiler should need to resolve the address of
std::endl. I realize that std::endl is templated, but surely it should be able to resolve the overload from the type of
the f argument to foo()...?
stymied by how to do this; here's a minimal program indicating my problem:
// test.cpp
#include <iostream>
void foo(std:stream& (*f)(std:stream&))
{
if (f == &std::endl) std::cout << "Detected 'endl'\n"; // line 5
}
int main()
{
foo(std::endl);
return 0;
}
// test.cpp
g++ (3.4.1) barfs on this with:
g++ -W -Wall test.cpp -o test.exe
test.cpp: In function `void foo(std:stream&(*)(std:stream&))':
test.cpp:5: error: address of overloaded function with no contextual type information
at the indicated line. I don't quite see what more information the compiler should need to resolve the address of
std::endl. I realize that std::endl is templated, but surely it should be able to resolve the overload from the type of
the f argument to foo()...?