1

Possible Duplicate:
Using Boost adaptors with C++11 lambdas

I would like to use Boost range adaptors to map (transform) a map to a list, through a C++11 lambda function. Like this:

  boost::copy(myMap | transformed([](pair<string, string> p){return p.first;}),
              ostream_iterator<string>(cout, ", "));

This won't work, because std::function does not have a value_type-type. I know that this particular transformation can be done with map_keys, but my point is more general. How can I use C++11 lambdas with Boost?

If it cannot be done without boilerplate, are there any plans to fix this issue? I have tried searching for it, but found suprisingly little.

Community
  • 1
  • 1
Gurgeh
  • 2,130
  • 15
  • 28
  • Note: C++11 lambdas and `std::function` are *two different things.* Your code doesn't use `std::function` at all. – Nicol Bolas Oct 01 '12 at 14:52
  • I thought that C++11 lambdas were compatible with std::function and, as such, are in some sense std::functions. – Gurgeh Oct 01 '12 at 15:19

1 Answers1

1

I found it myself! Starting with Boost 1.51.0, you add

#define BOOST_RESULT_OF_USE_DECLTYPE

Before including the boost files, and it will magically use decltype instead of ::value_type to infer return type. Now my example works!

Gurgeh
  • 2,130
  • 15
  • 28
  • 2
    Tread carefully now. This macro is a lot older than Boost 1.51 and has a tendency to cause trouble with older Boost Versions due to a number of problems. – pmr Oct 01 '12 at 15:21
  • I require 1.51 in my CMake-file. Is that enough, or are there still trouble ahead? Do you have a reference? – Gurgeh Oct 01 '12 at 15:30
  • 1
    There is this huge thread on boost.devel http://thread.gmane.org/gmane.comp.lib.boost.devel/233752/focus=234254 It boils down to a bug in decltype spec that got fixed late in the standardization, lots of compilers not conforming to the newest spec and many problems in boost where some specific behavior of `decltype/result_of` was assumed. You should be fine with Boost 1.51 and a completely compliant compiler. – pmr Oct 01 '12 at 15:35