(Note: I know Boost.Format, Iโm looking for a better way to do the following.)
First, an example of use: in some countries you name a person, first telling your last name, and the file name is the last, and in other countries the opposite.
Now, for my code, I am currently solving this using Boost.Format as follows:
#include <boost/format.hpp>
#include <iostream>
#include <stdlib.h>
#include <utility>
int main(){
using namespace boost;
int pos1 = 2, pos2 = 1;
char const* surname = "Surname", *forename = "Forename";
bool some_condition = false;
if(some_condition)
std::swap(pos1,pos2);
char buf[64];
sprintf(buf,"Hello %c%d%c %c%d%c",'%',pos1,'%','%',pos2,'%');
std::cout << format(buf) % surname % forename;
}
Now I would prefer it this way, that is, everything in the line format:
std::cout << format("Hello %%1%% %%2%%") % pos1 % pos2 % surname % forename;
But unfortunately this does not work as I get a nice parsing exception.
Is there any library for real positional formatting? Or even a way to achieve this with Boost.Format, which I don't know about?
source