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
  • 5,018
  • 4
  • 30
  • 52

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
  • 530,221
  • 131
  • 937
  • 1,214
1

% is an overloaded operator. Click here.

David G
  • 94,763
  • 41
  • 167
  • 253
mumu
  • 197
  • 7