I was looking at boost::format documents and just the first example made me wondering:
cout << boost::format("writing %1%, x=%2% : %3%-th try") % "toto" % 40.23 % 50;
What does it mean in terms of c++? I can understand call to boost::format itself, there's nothing strange there, but what's the meaning of the rest?
% "toto" % 40.23 % 50
Am I missing some special meaning '%' has in c++? What does this mean in terms of the language? I had a look into headers, but they're not particularly readable to be honest)
%, like many operators in C++, can be overloaded for arbitrary types. If we have the expressiona % bwhereais of typeAandbis of typeB, then C++ will look for functions compatible with the following signatures.So, presumably,
boost::formatreturns some custom class, on whichoperator%is defined. Theoperator%defined there takes this custom class and a string and produces a new instance of this custom class. Finally, when westd::cout << ...the custom class, Boost provides an overload for that as well which prints out the string representation.If we remove all of the operators and pretend everything is ordinary method calls, effectively this
becomes
And overload resolution on the names
pipeandpercenttakes care of the rest.