2

I came across this question, where the answer describes nice detail about how can the generic lambda functions be used to replace the std::function technique and how to rewire the stop condition to enable the return type deduction.

Based on the above I created the following working example:

#include <cstdio>

void printSeq(unsigned start) {
    auto recursion = [](auto&& self, const char* format, unsigned current) {
        printf(format, current);
        if(!current)
            return static_cast<void>(printf("\n"));
        else
            self(self, ", %u", current - 1);
    };

    recursion(recursion, "%u", start);
}

int main() {
    return (printSeq(15), 0);
}

My question is that what's the advantage using auto&& over the auto& in this case? Should I use the std::move here?

1
  • 1
    One thing to note is that since recursion doesn't capture anything, it's just a trivial object, so there will be no benefit from using std::move. Commented Jul 9, 2017 at 20:17

1 Answer 1

3

auto& is lvalue only.

This matters little until you refactor and replace the lvalue recursive object with a temporary proxy memoizer, for example.

auto&& is harmless, and means "I do not mind if this is a temprary or whatever, just don't make a copy", which expresses meaning well here. auto& states "No temporaries allowed!" Sometimes you want to exclude temporaries when making a reference, but it is rare.

auto const&, auto and auto&& should be your bread and butter.

Only use auto& if your operation is explicitly about writing and you are ok with excluding proxy references.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.