Standard no-op output stream

后端 未结 6 1821
余生分开走
余生分开走 2020-11-29 03:25

Is there a way to create an ostream instance which basically doesn\'t do anything ?

For example :

std::ostream dummyStream(...);
dummyStream <<         


        
6条回答
  •  借酒劲吻你
    2020-11-29 03:41

    If this is to disable logging output, your dummyStream would still cause arguments to be evaluated. If you want to minimize impact when logging is disabled, you can rely on a conditional, such as:

    #define debugStream \
        if (debug_disabled) {} \
        else std::cerr
    

    So if you have code like:

    debugStream << "debugging output: " << foo() << std::endl;
    

    No arguments will be evaluated if debug_disabled is true.

提交回复
热议问题