3

My tentative answer is no, as observed by the following test code:

#include <functional>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void TestFunc (void);
int TestFuncHelper (vector<int>&, int, int);

int main (int argc, char* argv[]) {
    TestFunc ();
    return 0;
} // End main ()

void TestFunc (void) {
    // Recursive lambda
    function<int (vector<int>&, int, int)> r = [&] (vector<int>& v_, int d_, int a_) {
        if (d_ == v_.size ()) return a_;
        else return r (v_, d_ + 1, a_ + v_.at (d_));
    };
    int UpperLimit = 100000; // Change this value to possibly observe different behaviour
    vector<int> v;
    for (auto i = 1; i <= UpperLimit; i++) v.push_back (i);
    // cout << TestFuncHelper (v, 0, 0) << endl; // Uncomment this, and the programme works
    // cout << r (v, 0, 0) << endl; // Uncomment this, and we have this web site
} // End Test ()

int TestFuncHelper (vector<int>& v_, int d_, int a_) {
    if (d_ == v_.size ()) return a_;
        else return TestFuncHelper (v_, d_ + 1, a_ + v_.at (d_));
} // End TestHelper ()

Is there a way to force the compiler to optimise recursive tail calls in lambdas?

Thanks in advance for your help.

EDIT

I just wanted to clarify that I meant to ask if C++11 optimizes recursive tail calls in lambdas. I am using Visual Studio 2012, but I could switch environments if it is absolutely known that GCC does the desired optimization.

Shredderroy
  • 2,860
  • 2
  • 29
  • 53
  • what compiler are you using; http://stackoverflow.com/questions/5231823/vs2010-c-tail-call-optimization?rq=1 seems to indicate that vs2010 can do some tail calls, or are you specifically referring to lambdas – Foon Oct 28 '12 at 23:08
  • 2
    The language, as far as I know, says nothing about this. If you're asking whether a particular compiler performs this optimization, you should tell us what compiler you're using (and probably rephrase the question). – Keith Thompson Oct 28 '12 at 23:08
  • I'm using Visual Studio 2012. Yes, as the question indicates, it appears that actual functions are being optimsed, but not lambdas. I was wondering if there was a compiler switch I could use to force the compiler to optimise lambdas. – Shredderroy Oct 28 '12 at 23:09
  • Sorry, I was missing the `using namespace std;` directive. I was also missing the forward declarations. Thanks for pointing out the errors. – Shredderroy Oct 28 '12 at 23:22

1 Answers1

9

You are not actually doing a tail-call in the "lambda" code, atleast not directly. std::function is a polymorphic function wrapper, meaning it can store any kind of callable entity. A lambda in C++ has a unique, unnamed class type and is not a std::function object, they can just be stored in them.

Since std::function uses type-erasure, it has to jump through several hoops to call the thing that was originally passed to it. These hoops are commenly done with either virtual functions or function-pointers to function template specializations and void*.

The sole nature of indirection makes it very hard for optimizers to see through them. In the same vein, it's very hard for a compiler to see through std::function and decide whether you have a tail-recursive call.

Another problem is that r may be changed from within r or concurrently, since it's a simple variable, and suddenly you don't have a recursive call anymore! With function identifiers, that's just not possible, they can't change meanings mid-way.

I just wanted to clarify that I meant to ask if C++11 optimizes recursive tail calls in lambdas.

The C++11 standard describes how a working program on an abstract machine behaves, not how the compiler optimizes stuff. In fact, the compiler is only allowed to optimize things if it doesn't change the observable behaviour of the program (with copy-elision/(N)RVO being the exception).

Xeo
  • 129,499
  • 52
  • 291
  • 397
  • 2
    It should follow from that that if you want to do recursive calls, a lambda expression may not be the best tool. – Luc Danton Oct 28 '12 at 23:29
  • An important admonition to keep in mind. I didn't remember running into such problems with `Func<>` in `C#`, so I may not have been looking out for the problem in C++. I should review my code. – Shredderroy Oct 28 '12 at 23:33