“Permanent” std::setw

Well, it’s not possible. No way to make it call .width each time again. But you can use boost, of course:

#include <boost/function_output_iterator.hpp>
#include <boost/lambda/lambda.hpp>
#include <algorithm>
#include <iostream>
#include <iomanip>

int main() {
    using namespace boost::lambda;
    int a[] = { 1, 2, 3, 4 };
    std::copy(a, a + 4, 
        boost::make_function_output_iterator( 
              var(std::cout) << std::setw(3) << _1)
        );
}

It does create its own functor, but it happens behind the scene 🙂

Leave a Comment