2

Could you, please, give an idea how to implement basic lambda expressions in C++03 without special libraries, in a simple, elegant and smart way? They should make it possible to do something like this:

for_each(some_vector.begin(), some_vector.end(), _first = -5)
sort(some_vector.begin(), some_vector.end(), _first > _last)

I have seen many topics on StackOverflow and in other places in the Internet, but, unfortunately, I didn't find something useful. Another idea is to look into Boost implementation carefully, but, sadly, my level is not very appropriate for it now.

Thank you in advance!

  • NO BOOST. But you can download it, and see its implementation. I think, it has elegant and smart implementation. If you cannot understand boost, then you cannot understand other implementation as well. – Nawaz Dec 12 '11 at 15:56
  • 2
    @Nawaz, op claims his ability is not at that level yet (heck, I look at the code sometimes and go wtf?) – Nim Dec 12 '11 at 15:57
  • Using boost would be ideal otherwise upgrade your compiler and use the standard lambdas. Reinventing the wheel is not something that even experienced programmers do often. – AJG85 Dec 12 '11 at 15:58
  • 3
    If you can't understand the code in boost, what makes you think you would understand our code? Increase your level. – Benjamin Lindley Dec 12 '11 at 15:59
  • @Nim: Yeah, I read that. So I edited my comment accordingly. – Nawaz Dec 12 '11 at 15:59
  • 4
    I don't think the Boost code is necessarily a good introduction to TMP. It is full of work-arounds for broken compilers and helper macros. Starting with something clean related to the topic seems easier. Arriving at a robust, portable solution is probably not the aim of the OP either. – pmr Dec 12 '11 at 16:01
  • Actually, I've implemented a simple version of Boost.Phoenix. I did this just out of fun and to feed my curiosity, but if I post that here, I've to explain it as well which scares the hell out of me, for it has lots of metaprogramming. – Nawaz Dec 12 '11 at 16:16
  • @Nawaz Commenting it thoroughly or making it into a small tutorial would be awesome, but also very time consuming. I'd certainly like to see it. – pmr Dec 12 '11 at 16:24
  • @pmr: If you want to see it, I can post it (after making it a bit short) but I wouldn't explain it probably; I don't have that much patience. – Nawaz Dec 12 '11 at 16:33
  • @Nawaz Sure, post it as an answer or edit it into mine as another example implementation. – pmr Dec 12 '11 at 16:44
  • Thank you! I think that pmr has pointed a good idea, and it was a one reason why I didn't want to read boost code carefully, because there are sometimes some not very simple constructions that were created for different compilers. I hope I was able to realize something like boost.lambda with the help of you and people in another topic: http://stackoverflow.com/questions/8501991/deduction-template-argument-c Thank you again! – Programmer585 Dec 22 '11 at 16:46

1 Answers1

5

AFAIK, the most common implementation technique for lambdas are expression templates.

This article gives an excellent introduction but you should really get one of the books on C++ TMP to get some further insight.

pmr
  • 58,701
  • 10
  • 113
  • 156