[ROOT] ROOT variable scope rules

From: William Hanlon (whanlon@physics.rutgers.edu)
Date: Tue Oct 16 2001 - 19:52:03 MEST


Hi,

I do not have a copy of the ANSI C++ rules in front of me, but I
believe that there are some discrepancies between ROOT and standard
C++ variable scope rules.

For example, I expect a variable declared in the body of an if
statement to be defined throughout the body of that statement like:

	  if (int foo = bar(arg))
	    {
	       cout << "foo is " << foo << endl;
	    }
	  else
            {
	       cout << " foo is 0 " << endl;
            }

This does not work in ROOT. The error returned is:

Warning: Automatic variable Int_tfoo allocated in global scope FILE:test.C LINE:12

but works exactly as expected when compiled using a c++
compiler

Additionally variables declared in a for loop exist outside of the
loop. Try running the following in ROOT and compile a version using
your favorite compiler and you will see a big difference between the
two outputs:

    int i = 66;

    for (int i = 0; i < 10; i++)
      {
        int k;
  
        k++;
  
        cout << "in loop k: " << k << endl;
  
      }
  
    cout << "outside loop i: " << i << endl;
    cout << "outside loop k: " << k << endl;

In fact this should not even compile using a compiler since k is not
declared outside of the loop. Additionally this program run through
ROOT will say that i is 10, but it should be 66 shouldn't it?

I have also run into problems with variables not being removed between
functions calls to the same function in loops.

Are these variable scoping differences intended, being worked on, or
am I just wrong about this? I haven't seen anyone else remark about
this in the roottalk digest.

Thanks for any comments,
Bill



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:03 MET