Hi All I am trying to build small logger library. I am facig some problem with c++ templates. Here is what my class structure looks like.
class abstract_logger_t {
public:
typedef abstract_logger_t logger_type;
template<typename data_t>
abstract_logger_t& log(const data_t& data) {
return *this;
}
};
class stdout_logger_t : public abstract_logger_t {
public:
typedef stdout_logger_t logger_type;
template<typename data_t>
stdout_logger_t& log(const data_t& data) {
cout << data << endl;
return *this;
}
};
template<typename logger_t, typename data_t>
void output(logger_t& logger, const data_t& data) {
static_cast<typename logger_t::logger_type&>(logger).log(data);
cout << data;
}
template<typename data_t>
abstract_logger_t& operator<< (abstract_logger_t& logger, const data_t& data) {
output(logger, data);
return logger;
}
stdout_logger_t logger;
logger << "Hi " << 1;
Here I am expecting stdout_logger_t::log to be called by output. but it looks like derived type is getting lost and abstract_logger_t::log ends up getting called. Can someone tell me if I am doing something wrong ?