Schroeder
Schroeder

Reputation: 880

Strange behavior when including <vector>

I see some very strange behavior when I include the standard vector library. Can anyone tell me what is causing these failures? Whose toes am I stepping on and is there a general case of this bug that I should watch out for?

I'm using the ROOT C++ interpreter. From either the command line interpreter interface or from compiled code,

$ root -l
root [0] #include <vector>
root [1] float max = -1.15;
root [2] if (max < -1 && max > -1.2) max = 2;
Error: Variable name has bad character 'max<-1&&max>-1207' (tmpfile):1:
Error: Symbol max<-1&&max>-1207 is not defined in current scope  (tmpfile):1:
Error: Variable name has bad character 'max<-1&&max>-1' (tmpfile):1:
Error: Symbol max is not defined in current scope  (tmpfile):1:
Error: non class,struct,union object $max<-1&&max>-1 used with . or -> (tmpfile):1:
*** Interpreter error recovered ***
root [3] max
(float)(-1.14999997615814209e+00)

Then if I add some parentheses that should do nothing:

root [4] if ((max < -1) && (max > -1.2)) max = 2; 
root [5] max
(float)2.00000000000000000e+00
root [6] .qqqqqqqqqqqq

If I just quit root normally, root seg-faults. If I don't include it works as it should:

[abarker@cmslpc29 macro]$ r
root [0] float max = -1.15;
root [1] if (max < -1 && max > -1.2) max = 2;
root [2] max
(float)2.00000000000000000e+00

Also, if I change the variable name to something other than "max" the problem vanishes.

Upvotes: 1

Views: 1180

Answers (2)

Pete Becker
Pete Becker

Reputation: 76245

It looks like the interpreter is seeing max as std::max, which is a template. That's why it's trying to treat the text inside < and > as a template argument. This is highly non-conforming; max is defined in the namespace std, so does not clash with the variable named max.

Upvotes: 0

Schroeder
Schroeder

Reputation: 880

max is being quietly defined by < vector >.

$ root -l
root [0] max
Error: Symbol max is not defined in current scope  (tmpfile):1:
*** Interpreter error recovered ***
root [1] max(1,2)
Error: Function max(1,2) is not defined in current scope  (tmpfile):1:
*** Interpreter error recovered ***
root [2] #include <vector>
root [3] max
(const void*)0x2b6758507c37
root [4] max(2,3)
(const int)3

So some bug in the interpreter is tripping over the overloaded name and messing up the scope rules.

Upvotes: 1

Related Questions