简体   繁体   English

C ++%运算符在这里做什么?

[英]C++ What is the % operator doing here?

I recently encountered code similar to this: 我最近遇到了类似于以下代码:

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

fmt % myString;

What is the (second) % operator doing here? (第二个)%运算符在这里做什么?

EDIT: 编辑:

I understand the end result, but I could not find a definition of how the % operator can be used like this. 我了解最终结果,但是找不到如何使用%运算符的定义。

Can someone provide an example that explains what exactly the meaning of the % operator is here? 有人可以提供一个示例来说明%运算符在这里的确切含义吗?

I understand the end result, but I could not find a definition of how the % operator can be used like this. 我了解最终结果,但是找不到如何使用%运算符的定义。

operator % can be overloaded. operator %可以重载。 Boost.Format does exactly that for its basic_format class : Boost.Format为其basic_format类完全做到了这basic_format

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

This member function gets invoked whenever you use the code fmt % value where fmt is of type boost::basic_format<Ch> . 每当您使用代码fmt % value其中fmt类型为boost::basic_format<Ch>时,都会调用此成员函数。

% is an overloaded operator. %是重载运算符。 Click here. 点击这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM