Leo jay wrote:
dear all,
i want to write log on disk file when macro NEED_DO_LOG is defined.
otherwise, do nothing.
so i wrote code like this:
#ifdef NEED_DO_LOG
fstream fsLogFile (...);
Shouldn't a log file be an ofstream rather than an fstream?
#else
CDummyStream fsLogFile;
#endif // #ifdef ASSAENGINE_NEED_DO_LOG
what i need to do now is making the CDummyStream acts like a fstream.
i wrote these code:
template <typename T>
CDummyStream& operator << (CDummyStream& dummy, const T& t)
{
return dummy;
}
above code can handle int, double, etc. but it can't handle the
function 'endl'.
if i write:
fsLogFile << endl;
the vs.net2003 compiler complains it could not deduce template argument
for 'overloaded function type' from 'overloaded function type'. what's
the meaning of that?
is that possible to pass endl as a parameter?
You need a few more overloads, to help the compiler with template
argument deduction (remembering that endl is a templated function, so
the compiler doesn't know which specialization to use when all that is
requested is a "T"):
inline CDummyStream& operator << (CDummyStream& dummy, std::ios_base&
(*manip)(std::ios_base&))
{
return dummy;
}
inline CDummyStream& operator << (CDummyStream& dummy,
std::basic_ios<char>& (*manip)(std::basic_ios<char>&))
{
return dummy;
}
inline CDummyStream& operator << (CDummyStream& dummy, std::ostream&
(*manip)(std::ostream&))
{
return dummy;
}
You should probably also make CDummyStream extend std::ostream, if it
doesn't already, and give it a NULL streambuf (e.g. construct the
std::ostream base class with std::ostream(NULL)), to be on the safe
side. Is threadsafety an issue for you?
Tom