I'd like to wrap cerr and cout inside an object that intentionally throws away everything in the release build. The intention is to make sure that some debug outputs that a developer might have forgotten to remove will not be shown to the user.
class Wrapper {
public:
Wrapper( std::ostream& os ):mOs(os){}
template <typename T>
DebugOnlyOsWrapper&
operator<<( T&& in ){
#ifndef NDEBUG
mOs << std::forward<T>(in);
#endif
return *this;
}
private:
std::ostream& mOs;
};
extern DebugOnlyOsWrapper dcout;
extern DebugOnlyOsWrapper dcerr;
However, I'm getting a "couldn't deduce template parameter 'T'" error when calling the operator as follows:
dcerr << std::endl;
What am I doing wrong? Is it not possible to deduce type from a function pointer?
Note that adding the following operator overloads fixes the problem, however I'd like to limit code duplication as well as understand the nature of the problem.
using CharT = std::ostream::char_type;
using Traits = std::ostream::traits_type;
Wrapper& operator<<(std::ios_base& (*func)(std::ios_base&) ){
#ifndef NDEBUG
mOs << func;
#endif
return *this;
}
Wrapper& operator<<(
std::basic_ios<CharT,Traits>&
(*func)(std::basic_ios<CharT,Traits>&)
){
#ifndef NDEBUG
mOs << func;
#endif
return *this;
}
Wrapper& operator<<(
std::basic_ostream<CharT,Traits>&
(*func)(std::basic_ostream<CharT,Traits>&)
){
#ifndef NDEBUG
mOs << func;
#endif
return *this;
}
Thank you