我最近遇到了类似于以下代码:

std::string myString = "test";
boost::format fmt("%s");

fmt % myString;


(第二个)%运算符在这里做什么?

编辑:

我了解最终结果,但是找不到如何使用%运算符的定义。

有人可以提供一个示例来说明%运算符在这里的确切含义吗?

最佳答案

我了解最终结果,但是找不到如何使用%运算符的定义。


operator %可以重载。 Boost.Format does exactly that for its basic_format class

template<class T>
basic_format&   operator%(const T& x)
    { return io::detail::feed<CharT, Tr, Alloc, const T&>(*this,x); }


每当您使用代码fmt % value其中fmtboost::basic_format<Ch>类型时,都会调用该成员函数。

09-05 09:01