Boost logo

Boost :

From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2007-11-27 12:10:02


On 11/27/07, Alexander Nasonov <alnsn_at_[hidden]> wrote:
>
> After few months of inactivity I came back to a local function syntax. Instead
> of trying to bind local variables, I implemented a couple of macros that simply
> assign a value to a function pointer:
>
> int BOOST_LOCAL_FUNCTION(char c)
> {
> std::cout << c << '\n';
> return 1;
> } BOOST_LOCAL_FUNCTION_DECL(a)
>

What is the advantage of this with respect to:

struct unnamed { static void _ (char c) {
         std::cout << c << '\n';
         return 1;
}};

(&unnamed::_)('a');

It is ugly, but I find the BOOST_LOCAL_FUNCTION_DECL macros even
uglier. The macros would make sense only if they made it
easy to close around the environment.

Lately I've been using local structures like the 'unnamed' above to
create 'lambdas'
for standard algorithms instead (or in addition to) Boost.Lambda. They
work very well in practice:

  std::vector<std::vector<int> > v = ...;

  struct pred { static bool _ (std::vector<int> const& x, int max_sum) {
       return std::accumulate(x.begin(), x.end()) < y;
  }};
  v.erase(std::remove_if(v.begin(), v.end(), boost::bind(&pred::_, _1,
10)), v.end());

Easier to use than lambda and, most importantly expecially with older
compilers, compile faster!

--
gpd

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk