Re: std::iostream help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



How many different data types are sent to Debug_out in those millions of places and how many of those types are themselves wrappers?

It is quite easy to create a class that contains a std::iostream as a member and wraps the behavior of std::iostream.

The direct way to do that is to provide a templated definition of operator<<() in that wrapper class that checks the debug level and (by run time conditional) redirects all the work to the contained iostream.

That method works until it conflicts with the templated operator<<() definitions inside data wrapper types that are passed to it.

The data wrapper operator<<() typically redirects all << operations to the wrapped data regardless of the stream type, just as your operator<<() redirects all << operations (after the debug level test) to the wrapped stream.

But the compiler can't decide which operator<<() template to chose. Choosing either one would generate the same correct behavior, but so far as I understand there is no way to tell the compiler to choose a specific one or to choose on its own, or to work at all in this case. It just rejects the code because of the ambiguity.

If you are using no such data wrappers, the stream wrapper you want is easy.

If you are using few (by type) such data wrappers (even if many by instance) you can individually code non templated overrides for operator<<() for the problem combinations.

Brian McGrew wrote:
I need a wrapper to use around std::iostream...

Right now, I have this:

std::iostream debug_out;

And I use it the same way you¹d use std::cout...

Debug_out << ³Debug Message² << std::endl;

And it works fine.

But, debug_out gets used in millions of places in my code, so, somehow I
need to come up with a wrapper around the std::iostream to check the debug
level...


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux