Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using boost::format in a boost::lambda

For some reason, I fail to use boost::format in a boost::lambda. Here is a (hopefully) compilable simplification of my code :

#include <algorithm>
#include <iomanip>
#include <iostream>

#include <boost/assign/list_of.hpp>
#include <boost/format.hpp>
#include <boost/lambda/lambda.hpp>

namespace bl = boost::lambda;

int main()
{
    const std::vector<int> v = boost::assign::list_of(1)(2)(3);
    std::for_each(v.begin(), v.end(), bl::var(std::cout) << std::setw(10) << bl::_1);
    std::for_each(v.begin(), v.end(), bl::var(std::cout) << boost::format("%10d") % bl::_1);
}
  • The first std::for_each produces the expected output
  • The second std::for_each only outputs whitespaces without any number

Why is that ? I'm really not familiar with boost::lambda so I might be missing the obvious here.

Please do not suggest std::copy based answers : my actual code does not work on std::vector but on boost::fusion::vector (and std::for_each is in fact a boost::fusion::for_each).

like image 350
icecrime Avatar asked Dec 07 '10 21:12

icecrime


2 Answers

For some reason, your code evaluates boost::format("%10d") % bl::_1 immediately, rather than on each invocation of the lambda.

To prevent this, you need to wrap boost::format("%10d") in a call to bl::var, just as you have done with std::cout.

Unfortunately, doing this requires Boost.Lambda to deduce the return type of the call to operator%, which it is unable to do. Therefore the return type must be specified explicitly, using bl::ret. Note that this return type must be a reference, in order that std::cout accesses the returned object directly rather than a copy of it.

We thus get the following code, which produces the expected output:

std::for_each(v.begin(), v.end(), bl::var(std::cout) <<
    bl::ret<const boost::format &>(bl::var(boost::format("%10d")) % bl::_1));
like image 156
user200783 Avatar answered Nov 15 '22 00:11

user200783


My bet is that you're running into the fact that a format used is no longer usable.

boost::format f("...");

std::string s = f % ... ;
std::string s2 = f % other options...; // FAIL!  f has been changed by the above use!

In other words, using % on a format actually replaces the string data with whatever you %'d into it. The cooler thing is that the second use above will silently fail.

I know, kind of counter-intuitive, but it is what it is.

like image 21
Edward Strange Avatar answered Nov 15 '22 00:11

Edward Strange