How to do dynamic scoping

How come not able doing a dynamic scoped variable, how to do dynamic scoping in c++ as this illustration fails:

void f(int i);

void fn(int& m){
int i;
f(i);
}

void f(int i){
int k; k=m;
}


is like

: error: use of undeclared identifier 'm'
How to solve to the correct end? thanks before
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).
You could let the caller pass m as an argument.

1
2
3
4
5
6
7
8
9
10
11
void f(int i, int m){
  int k = m;
  ...
}

void fn(int& m){
  int i;
  ...
  f(i, m);
  ...
}

Last edited on
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;
}

...
foo(100);
foo(42);
k = foo(0);

what is k? how exciting!
Last edited on
There are basically two ways to properly scope a variable in one function so it is visible in another.

1, Make the variable a global. Not recommended, but it is a common newbie method.

2. Pass the variable into the second as a parameter, pointer or reference. Preferred.

Someone really needs to stop trying to mangle C++ to do things it was never designed to do.
Core Guidelines:
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.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-capture-vs-overload


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void foo( int& a )
{
    std::cout << "foo entry: a == " << a << '\n' ;

    int i = 10 ;

    const auto fn = [&] ( int x ) { a *= x ; std::cout << "foo::fn: foo::a == " << a << '\n' ; } ;

    const auto fn2 = [&] { a += i ; std::cout << "foo::fn2: foo::a == " << a << ", foo::i == " << i << '\n' ; fn(i) ; } ;

    fn2() ; // call fn2's closure fn2 => fn

    std::cout << "foo exit: a == " << a << '\n' ;
}

int main()
{
    int a = 22 ;
    foo(a) ;
}

http://coliru.stacked-crooked.com/a/4fc54c2c54641104
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 ++
Last edited on
Topic archived. No new replies allowed.