3

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

const hasn't been 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 like the compiler should know, in most cases, when a function could be const.

It has also been written in standard C++ as of the early 2000s. I would like to change to use auto and for range loops so

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

to

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

I could hack something together with perl and/or sed that would probably find most of the cases because the code conforms closely to 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 may want to do? How do you migrate legacy C++ code to a new standard?

user3642186
  • 179
  • 5
  • 1
    Don't use int foo( const int &a ), just int foo(int a). – Wojtek Surowka Aug 26 '14 at 23:48
  • 11
    Don't change what isn't broken... – M.M Aug 26 '14 at 23:59
  • I _highly_ recommend not doing the loop bit automatically. There's a lot of loops where you actually need iterators for various reasons. – Mooing Duck Aug 27 '14 at 00:00
  • Yeah, the loop syntax part might be a little hairy. If you have any loops that need to erase elements while iterating through a container you are pretty much forced to use iterator syntax over range based for loops. – Sir Digby Chicken Caesar Aug 27 '14 at 00:26
  • @MooingDuck: The cases when you actually need the iterator are pretty easy to detect, you have `it` appear inside the body in some form other than `*it` or `it->` It's more difficult to decide between `auto elem`, `auto& elem`, or `auto const& elem`. – Ben Voigt Aug 27 '14 at 00:32
  • Note that automated detection and suggestion is useful, but you don't want automated changes without a set of skilled eyes approving or rejecting each change. Applying this blindly will make code less generic. – Ben Voigt Aug 27 '14 at 00:37
  • I'm not sure `foo` should be a member function of `A` at all. If it is, it should be `static`. – Chris Drew Oct 10 '14 at 19:07
  • possible duplicate of [How can I find C++ functions that should be const?](http://stackoverflow.com/questions/25539590/how-can-i-find-c-functions-that-should-be-const) – Chris Drew Oct 10 '14 at 19:11

1 Answers1

0

Your problem seems to be a great opportunity to use ReSharper for C++ in case your code is organized into Visual Studio solution. It is still in development but thanks to this fact it is free.

It contains a lot of built in code inspections and you can perform solution wide code cleanup. I use its C# version on daily basis and while C++ version has still lots to catch up it seems to be a suitable tool for your situation.

Michal Hosala
  • 5,570
  • 1
  • 22
  • 49