c++ - Correct way to declare/define custom cout-like object -
i created own std::cout-like object writes both std::cout , log file.
i'm defining in header file, i'm getting unused variable warnings.
header file <mylib/log.h>
static lout { }; static lout lo; template<typename t> inline lout& operator<<(lout& mlout, const t& mvalue) { std::string str{tostr(mvalue)}; std::cout << str; getlogstream() << str; return mlout; } usage:
#include <mylib/log.h> ... lo << "hello!" << std::endl; should lo static? should lo extern?
kudos explaining correct way of declaring cout-like object , showing how main standard library implementations it.
edit: cout-like object, mean global variable available after including corresponding header.
std::cout declared follows:
namespace std { extern ostream cout; } it regular global variable; can same thing yourself. put extern declaration of variable in header; define same variable in source file , link application:
// mylog.h extern mylog mylog; // mylog.cpp mylog mylog(someparams);
Comments
Post a Comment