1

Suppose my class has one method which has to call some other recursive method from inside it.

My .h file would look like this:

class MyClass() 
{
public:
    void foo();
};

My question is, which of these implementations would be better (ignoring the infinite loops):

namespace
{
    void myRecursiveFunction()
    {
        myRecursiveFunction();
    }
}
void MyClass::foo()
{
    myRecursiveFunction();
}

or

void MyClass::foo()
{
    std::function<void()> myRecursiveFunction =
        [&] ()
    {
        myRecursiveFunction();
    };

    myRecursiveFunction();
}

, giving that myRecursiveFunction()will only be called from foo() ?

Of course, in the real world myRecursiveFunction() does something. I just wanted not to populate the code with unneeded information.

Is it ok for all my class to have access to myRecursiveFunction if I only use it in one method?

  • Does the second example even compiles? Why would you want to create an additional layer of inference for nothing? I don't get the *point* of your question. – YSC Dec 21 '15 at 09:33
  • @YSC Presumably it would be not-nothing: there is a private-to-something function that is invoked from said something. – user2864740 Dec 21 '15 at 09:35
  • 1
    @YSC Anyway, wrt recursive lambdas: http://stackoverflow.com/questions/2067988/recursive-lambda-functions-in-c11 , http://stackoverflow.com/questions/14531993/can-lambda-functions-be-recursive – user2864740 Dec 21 '15 at 09:38
  • @YSC I fixed the compilation problems. I was wondering if it's ok to have ```myRecursiveFunction``` accessible by all my class, since it is only used in one method. – Alexandru Kis Dec 21 '15 at 09:41
  • With the capture by reference in the second case, the two functions are not the strictly equivalent, so your question is biased. – Holt Dec 21 '15 at 09:53

2 Answers2

2

Yes it's OK to define myRecursiveFunction() in an anonymous namespace, even if its visible to all your class' implementation, even if there is only MyClass::foo() who calls it.

Presumably, you control your class implementation, and you won't call myRecursiveFunction() where there is no reason to. This goes for all functions: would you call abort() where you should not? Though it's accessible by your whole class.

Using a recursive local lambda only adds a layer of inference and makes the code more complex to read. If this is a time-critic part of your program, you probably make it slower by using an std::function.

YSC
  • 38,212
  • 9
  • 96
  • 149
2

std::function pros:

  • If the recursive function is only called from that function, then it's scope is well limited. The anonymous namespace is visible to the whole translation unit. It's OK for the function to be visible to the rest of the tranlation unit, so this is arguably only a slight advantage.

std::function cons:

  • A simple function has simpler syntax than a lambda.
  • std::function would probably have some small overhead.
  • If you define the std::function inside foo, then the recursive function is not reusable outside it. Which is fine, if it's not reusable anyway.
  • Won't work in a pre-c++11 standard.

Which is better depends on what you need and what you prefer. I would prefer a simple function in most cases.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326