0

I have defined a function inside of another function. I would like to access a variable in the outer functions scope while in the inner functions scope. I do not want to pass the variable x into the function, I want for it to be known.

#include <iostream>

int main() {
    double x = 1;
    auto f = []() {std::cout << x << std::endl;};

    return 0;

} // end main

Is this possible?

Thanks for your time.

GeneralCode
  • 444
  • 3
  • 13
  • 1
    Does this answer your question? [How can I access local variables from inside a C++11 anonymous function?](https://stackoverflow.com/questions/7100889/how-can-i-access-local-variables-from-inside-a-c11-anonymous-function) – t.niese Jul 12 '20 at 17:49
  • Not exactly the same thing @t.niese. Also, its not very minimal, hard to understand at first glance. I don't agree at all that the post you cite is a duplicate. It is cryptic. (This is the first time I've said no to a duplicate request.) – GeneralCode Jul 12 '20 at 17:51
  • 1
    It is the same thing the `[](float x)->float{return(x/tot);}` is the lambda and the questions asks, how to access the variable `tot` in the lambda, that's the exact same situation. And the answer describes how those variables can be captured. You could in fact just copy and past `C++11 lambdas support capturing by:` and everything after that to your question as an answer. – t.niese Jul 12 '20 at 17:54
  • It is not clear at first glance, and is no where near minimal. The function is not shown inside of another function. Too many words, too complicated code. @t.niese – GeneralCode Jul 12 '20 at 17:56
  • [What is a lambda expression in C++11?](https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11/7627218#7627218) – t.niese Jul 12 '20 at 18:08
  • @t.niese I agree with you that this is a subject which has been discussed before. However, I do not agree that a user coming from Python would land on either of the splash pages you have attached. Also, given your wondrous history of help to users of stack exchange, I would be willing to wager you believe my post is a https://stackoverflow.com/help/minimal-reproducible-example. – GeneralCode Jul 12 '20 at 18:16
  • A duplicate does not need to be an exact duplicate. Furthermore, it does not hurt to link other relevant questions to yours, in case someone stumbles across your question, that person has further questions to read about that topic. And your question appears as **linked** questions in the others. And I didn't say that your question is non a minimal example, but it is still a duplicate. – t.niese Jul 12 '20 at 18:36
  • Thank you for linking the other questions, I agree there is extended discussion on this subject in other places. However, I still disagree that it is a duplicate. – GeneralCode Jul 12 '20 at 18:38

2 Answers2

3

This is called lambda capture and it's what the leading square brackets in a lambda expression are about. You basically have two options:

  1. By-value capture:
auto f = [x](){ std::cout << x; };

Lambda object receives its own private copy of the object so any subsequent changes to the outer x have no effect on the lambda. A special capture = captures by-value every free variable in the lambda's code not captured before it:

double x = 40, y = 2;
auto f = [=]{ return x + y; };
  1. By-reference capture:
auto f = [&x](){ std::cout << x; };

Lambda object accesses an outer scope object by reference, so the subsequent changes to the outer x become known to the lambda. A special capture & captures by-reference every free variable in the lambda's code not captured before it:

double x = 40, y = 0;
auto f = [&]{ return x + y; }; 
y = 2;
bipll
  • 11,131
  • 1
  • 15
  • 31
2

Use & as shown below.

auto f = [&]() {std::cout << x << std::endl;};

[&] - is default capture mode. This way of doing can capture outside variables that are referred inside lambda function by reference.

If you want to capture the outside variables by value, you should use [=].

There are many other places with great explanation. But lot of documentation gives too much details where we loose our self. So I am preferring to post microsoft documentation link, which is easy to understand.

https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=vs-2019#:~:text=A%20lambda%20begins%20with%20the,it%20are%20accessed%20by%20value.

Pavan Chandaka
  • 8,891
  • 4
  • 17
  • 29
  • 1
    Thank you sir, this is precisely what I needed. I up-voted your answer and will mark this as correct once it allows me to do so. Someone down voted your answer, but did not provide a reasoning of any kind... – GeneralCode Jul 12 '20 at 17:46
  • 1
    I didn't downvote, but maybe an explanation of what the `&` does would make this a better answer. (I didn't upvote, either, *yet*.) – Adrian Mole Jul 12 '20 at 17:48
  • Yes! What is the and person here (&)? Why does it work? What is it called? Where is a related article. Good point @AdrianMole I was thinking the same thing. – GeneralCode Jul 12 '20 at 17:49