c++boost-lambdaboost-format

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

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


Solution

  • 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));