Find and mark const variables and const functions in C ++

I have a large C ++ code base (500,000 + LOCs, 10k files) and I am happy with the code, but there are several problems with it:

const is not used as much as it should. For example:

class A
{
  int foo( B &a )
  {
    return a.v * 10;
  }
};

      

should really be changed to:

class A
{
  int foo( const B &a )  const
  {
    return a.v * 10;
  }
};

      

It seems the compiler should know, in most cases, when a function can be const.

It has also been written in standard C ++ since the early 2000s. I would like to change the use of auto and ranges so that

 std::map< int, std::string >::iterator i = m.start();
 for ( ; i != m.end(); i++
   ...;

      

to

for ( const auto p : m )
  ...;

      

I could hack something along with perl and / or sed, which will probably find most cases because the code follows our internal standards, but I'm just wondering if there is a real tool out there that will do this and other things that we might want to do? How to port legacy C ++ code to the new standard?

+3


source to share


1 answer


Your problem seems like a great opportunity to use ReSharper for C ++ in case your code is organized into a Visual Studio solution. It's still in development, but thanks to that, it's free .



It contains many built-in code checks and you can clean up your solution code. I use his C # version on a daily basis, and while the C ++ version still has a lot to catch up with, it seems like the right tool for your situation.

0


source







All Articles