I recently got the following warnings when compiling ROOT libraries with my code:
TUUID.h: In function ‘TBuffer& operator<<(TBuffer&, const TUUID&)’:
TUUID.h:97: warning: cast from type ‘const TUUID*’ to type ‘TUUID*’ casts away constness
TDirectory.h: In member function ‘virtual void TDirectory::SetMother(const TObject*)’:
TDirectory.h:182: warning: cast from type ‘const TObject*’ to type ‘TObject*’ casts away constness
TROOT.h: In member function ‘void TROOT::SetSelectedPrimitive(const TObject*)’:
TROOT.h:252: warning: cast from type ‘const TObject*’ to type ‘TObject*’ casts away constness
Iam using ROOT 5.21/06 and the following g++ command:
So, the answer - it depends on how you use this code. If you do not use the members your compiler mentioned, there is nothing you can do and you can ignore these messages from compiler.
[quote]Thanks, so the only question remaining is: Should this be fixed in future releases?
Cheers,
Stefan[/quote]
Good question. I’m not sure how much code depends on TDirectory’s interface. (I mean how many modifications should be done).
For me, it is a stupid warning - if I do explicit cast, I usually know why, so I do not need any screams from compiler. But some option (-Wcast-qual ???) requests compiler to do this. So, this is a dilemma: to switch-off this option, or to fix the code?
Or simply ignore these warnings since you know, they are “false”?
If it was me, who have to change something, I would simply ignore warnings.
Ok, of course I can switch off these warnings, but then I can not check my own files either
I can also stick to this compiler option, but then I have to “ignore” those messages from the ROOT libraries. But to be honest, the messages are a little bit annoying when one is looking for other “real” warnings in the own code. So I think the point is: If there is no need to cast const objects one should fix this in the code.
But maybe Rene can clarify a bit on this…
Yes, that’s true.
The simplest solution would be to remove definitions for these member-functions from *.h into *.cxx. So, when you compile your own code, which includes ROOT’s headers (and links agains ROOT’s libraries), you won’t have such warnings and can use specific compiler options.
IMHO this solution is the best - NO code must be changed in fact, so, no code, which depends on these base classes and their interfaces will be broken.
But there is a need, since, for example fMother in TDirectory is TObject * and you cannot assign const TObject * to TObject *.
A bit of history. Initally, SetMother in TDirectory has parameter of type TObject *.
But in version 3.x.x of ROOT a lot of changes were done to add const (function qualifiers, changes in parameters etc.):