[cfe-users] Is there a compiler warning for breaking deep const?

Brian Cole coleb at eyesopen.com
Thu Feb 14 11:34:41 PST 2013


Does clang have a compiler warning for when code could be breaking deep const-ness? I know this is legal in C++, but it is often undesirable, especially in light of const now implying thread safety in most libraries. We just chased a thread safety bug down to a problem like the following: 

struct Foo
{
  void DoSomething();
};

class Bar
{
  Foo *foo;
  void DoConstSomething() const { foo->DoSomething(); }
};

If clang doesn't have this warning, it would be extremely useful to have as we migrate to a concurrent world. I would propose that to work around the warning the user could do one of the two following things: 

class Bar
{
  mutable Foo *foo;
// OR the following const_cast
  void DoConstSomething() const { const_cast<Foo *>(foo)->DoSomething(); }
};

What makes this even trickier is that the std smart pointer classes allow you to do the same thing. I don't know if clang could also warn about these as well, but having the former would be a good first step in the right direction. 

Thanks,
Brian



More information about the cfe-users mailing list