Compiler warnings when using ROOT classes

Hi,

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:

g++ -g
-ansi
-fno-default-inline
-ffor-scope
-Wall
-W -Wpointer-arith
-Wcast-qual
-Wcast-align
-Wconversion
-Woverloaded-virtual
-Wsign-compare
-Wnon-virtual-dtor
-Woverloaded-virtual
-Wfloat-equal
-Wno-reorder
-D_GNU_SOURCE

Can anybody tell me wether this is something to care about?

Cheers,
Stefan

Casting away const is bad, if object is really const:

const int o = 10;
int & ro = const_cast<int &>(o);
ro = 10;//Undefined behavior

But it’s not a problem, if real object is not const:

SomeClass c;
const SomeClass *pcc = &c;
SomeClass *pc = (SomeClass *)pcc;
pc->i = 10;
pc->NonConstMemberFunction();

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.

Thanks, so the only question remaining is: Should this be fixed in future releases?

Cheers,
Stefan

[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. :slight_smile:

Hi!

Ok, of course I can switch off these warnings, but then I can not check my own files either :wink:
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…

Thanks a lot,
Stefan

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.):

root.cern.ch/viewvc/tags/v3-00-0 … h?view=log

And here the parameter’s type became const TObject *

root.cern.ch/viewvc/tags/v3-00-0 … iew=markup

root.cern.ch/viewvc/tags/v3-00-0 … 08&r2=1205

Hi,

We are working on removing those warnings where-ever possible.

Cheers,
Philippe.

Hi,

Most of the ‘const’ warnings have been removed from the header files.
Please let me know if you still have problems.

Cheers,
Philippe.