samedi 31 octobre 2015

Getting Started with Boost (Using lambda namespace)

I recently installed boost and have been trying to understand how everything works. The boost.org page gives

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;
    std::for_each( in(std::cin), in(), std::cout << (_1 * 3) << " " );
}

as a first code which is supposed to use the lambda namespace to multiply the input stream by 3. I'm really not used to this notation (I've always put using namespace std in my code and didn't worry about the intricacies going on in the background), and although I partly understand this code, it would be great if someone can break it up line to line and provide an explanation. For example, how does the compiler know that _1 is taken from the lambda namespace, and why do we have to specify std:: before cin and cout. Can we do without having to prefix std:: each time we use a boost library?

Aucun commentaire:

Enregistrer un commentaire