Public Group

# a question about cout

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

## Recommended Posts

i have an app which i use the console for debugging but when when i distribute it i'm planning on taking off the console. If i have a statement like: cout << "some debug info" << info << endl; and i have the console turned off. Will it take up CPU Time? Do you think its worth it to remove these debug statements when i release it?

##### Share on other sites
It will take CPU time. Whether or not it is relevant depends on where the code actually is (during the startup sequence? in a tight loop?)

A solution would be to use a differently-named object for logging, such as debug. Then, depending on whether you're in a debug mode or in a non-debug mode, either bind that object to std::cout (or, as a personal preference, std::clog), or create a dummy ignored class for that object, and define an output operator which does strictly nothing, and will therefore be completely eliminated by the compiler:

#ifdef NDEBUG  class ignored {};  ignored debug;  template<typename T>  ignored & operator << (ignored & i, const T & other)  {    return i;  }#else  std::ostream & debug = std::clog;#endif// Later in sourcedebug << "Some debug info " << info << std::endl;

Of course, this has the advantage of not breaking existing code which outputs the results of mandatory function calls (the display is not performed anymore, but the function call still is performed).

##### Share on other sites
Use the #if directive :

#ifdef _DEBUG
cout << "some debug info" << info << endl;
#endif

The compiler will evaluate the code between #if and #endif only in debug mode.

##### Share on other sites
Quote:
 Original post by KambizUse the #if directive :#ifdef _DEBUG cout << "some debug info" << info << endl;#endifThe compiler will evaluate the code between #if and #endif only in debug mode.

Peppering your source with precompiler directives causes source-bloat and confusion. A better alternative would be to wrap the directive conditional inside an inline function.

• 10
• 12
• 23
• 10
• 19