intercepting ostream manipulators

This topic is 4321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi guys, I'm hoping one of you C++ pro's will know the answer to this. Consider the following:
class myostr : public std::ostream
{
public:
...

myostr & operator << (std::ostream & (*f)(std::ostream &))
{
if( f == std::endl )
// do something here
return *this;
}
private:
...
};


What is wrong with that if statement? In VC++ .NET 7 it compiles clean. In XCode 2.4 (?? GCC some version of other ??) I get the following compilation error: error: assuming cast to type 'std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&)' from overloaded function I'm assuming because the endl is a function template and not specialized, so how can the compiler made comparisons to f? Thanks, metrix

Share on other sites
Quote:
 Original post by metrixI'm assuming because the endl is a function template and not specialized,so how can the compiler made comparisons to f?

Cast the expression std::endl to the appropriate type before doing the comparison. This will force the selection of one of the overloads, as determined by the type you are casting to.

Share on other sites
Can you perhaps show me how to do this?
I have tried various casts, but with the template
syntax I'm quite lost.

metrix

Share on other sites
Maybe something like this: static_cast <std::ostream & (*)(std::ostream &)> (std::endl)?

Share on other sites
that did the trick! thank you.

metrix

1. 1
2. 2
frob
16
3. 3
Rutin
12
4. 4
5. 5

• 13
• 12
• 59
• 14
• 15
• Forum Statistics

• Total Topics
632124
• Total Posts
3004244

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!