我正在实现一个使用流操作符的日志记录类。基本思想是自定义类型可以实现一个operator<<
为记录提供可读的表示。日志记录类将收集各种消息并将它们(作为单个日志记录条目)转发(销毁到系统日志或其他)。运算符<<超载(只)为std :: stringstream dervied类
class log_stream : public std::ostringstream
{
inline ~log_stream()
{
forward_to_log(str().c_str());
}
};
class custom_type
{
};
std::ostream &operator<<(std::ostream &stream, const custom_type &)
{
stream << "<custom_type data>";
return stream;
}
log_stream() << "error in custom type: " << custom_type_variable;
这实际上工作得很好,除非语句不从的std :: ostream的过载启动,但自定义类型,而不是直接:
log_stream() << custom_type_variable; // no known conversion from 'log_stream'
// to 'basic_ostream<char, ...>&
// for 1st argument
现在我不知道为什么,因为log_stream
是-a ostringstream
is-a basic_ostringstream
is-a basic_ostream
。有任何想法吗?
此外:是否有任何方式为log_stream&
而不是直接提供operator<<
重载std::ostream
(如果一个人想用于记录两种不同的过载 - 与log_stream
使用 - 和例如序列化的盘 - 与fstream
使用) ?
EDIT#1
如果 '能够r值' operator<<
被添加的第一个问题就解决了。
template <typename Type> inline log_stream &operator<<(log_stream &&stream, Type&& type)
{
return operator<<(stream, std::forward<Type>(type));
}
然而现在/静止的类型转换为基类分解(无论是ostringstream
或ostream
)。
log_stream() << custom_type(); // OK
log_stream() << custom_type() << "text"; // OK
log_stream() << "next"; // non-const lvalue reference to type 'log_stream' cannot bind
// to a value of unrelated type 'basic_ostream<char, ...>'
为什么basic_ostream<char, ...>
类型无关?它是一个基类log_stream
它应该有可能在这里得到这个基类的引用,不是吗?
编辑#2
那么,它当然应该调用成员operator<<
,这使得它的工作。
template <typename Type> inline log_stream &operator<<(log_stream &&stream, Type&& type)
{
stream << std::forward<Type>(type);
return stream;
}
所以问题解决了C++ 11 - 但它仍然没有为C++ 03(哎呀)工作。
想到的一个解决方案是提供一个“r值给l值换算操作符”,其最短形式为operator()
。
class log_stream
{
inline log_stream &()()
{
return *this;
}
}
log_stream()() << custom_type() << "text";
不漂亮,但有些东西。任何更好(更漂亮)的想法?
您应该从'streambuf'派生,而不是流类型。阅读http://gabisoft.free.fr/articles/fltrsbf1.html – aschepler 2013-04-21 13:44:59
这对实际问题没有影响。 – 2013-04-21 14:59:35
广告附加(编辑)问题:stream << type'的返回类型是'basic_ostream',所以你需要执行一个'static_cast'回到你的派生类型。例如:'return static_cast(stream << std :: forward (type));' –
dyp
2013-04-21 17:58:37