【问题标题】:boost::format() output operatorboost::format() 输出运算符
【发布时间】:2011-02-27 12:44:40
【问题描述】:

我需要编写一个类,该类将通过像 cout 那样的运算符接受 boost::format()

cout << boost::format("some string; some param = %d\n") % someValue;

运算符的参数必须是哪种类型? 这样该类将像这样声明:

class Output
{
    Output& operator<<(... format);
}
int main()
{
    Output output;
    output << boost::format("...");
}

谢谢。

【问题讨论】:

    标签: c++ string-formatting boost-format


    【解决方案1】:

    boost::format 是您想要的类型。它有一个 str() 成员来获取 std::string。

    【讨论】:

    • 但我不明白 cout 是如何知道某些 boost::format 类型的?
    • @Slav cout 知道一些 boost::format。在 boost 中,有一个 operator
    • 没有。不需要继承。这只是operator&lt;&lt; 的另一个重载。它看起来像 ostream&operator&lt;&lt;(ostream&amp; out, const boost::format&amp; fmt) { out &lt;&lt; fmt.str(); return fmt; }。同样,operator&lt;&lt; 可以是免费功能,它不必是成员。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2012-03-12
    相关资源
    最近更新 更多