0

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?

mclaassen
  • 4,822
  • 3
  • 28
  • 51

2 Answers2

3

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. 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); }

This member function gets invoked whenever you use the code fmt % value where fmt is of type boost::basic_format<Ch>.

Konrad Rudolph
  • 506,650
  • 124
  • 909
  • 1,183
1

% is an overloaded operator. Click here.

David G
  • 90,891
  • 40
  • 158
  • 247
Mou
  • 188
  • 6