Not sure what to say, other than the obvious: m is local to the 'fn' function. It does not exist in the 'f' function. What you're asking for is not within the C++ language (nor should it be).
m is a reference to something we can't see. maybe you can pass the real thing around to k?
that aside,
you cannot override the scoping rules of the language.
you can do all sorts of things to work around that, from globals to static class members to dynamic memory tricks, but none of those break the scope rules they just dodge them by moving the scope around. you can make a static local and grant access to it externally, too, but that is just asking for trouble.
int &foo(int x)
{
static int duh{x};
return duh;
}
Use a lambda when a function won’t do (to capture local variables, or to write a local function)
Reason: Functions can’t capture local variables or be defined at local scope; if you need those things, prefer a lambda where possible, and a handwritten function object where not. On the other hand, lambdas and function objects don’t overload; if you need to overload, prefer a function (the workarounds to make lambdas overload are ornate). If either will work, prefer writing a function; use the simplest tool necessary.
Respectfully @abdulbadii I don't understand why you don't want to use tag in order to post properly your request. It is really annoying after a few weeks... It is a matter of respect. And sometimes you can simply say "thank you" to those who helped you. I am not a moderator here, but I don't understand your behavior. Don't be angry against me, it is just my point of view which you can freely despise. I wish you the best. Have a good day ++