Amit Kumar
Amit Kumar

Reputation: 93

How to port existing C++ code to C++11

We are working on a module that is developed in C++, but given the new C++11, I am thinking about migrating to that.

How to proceed? Are both the same or is there some compiler dependency?

My software currently supports Windows and Linux. I am using Microsoft Visual Studio as well as GCC to build it.

Overall, what changes are needed if any?

Upvotes: 7

Views: 3129

Answers (4)

Ali
Ali

Reputation: 58491

As others have pointed out, your code will probably compile just fine. If you are curious what could go wrong, then see

What breaking changes are introduced in C++11?

If you are planning on changing your old code to use C++11 features, I would add auto to Baget's answer.

Upvotes: 2

Balog Pal
Balog Pal

Reputation: 17183

Migration? I thought WG21 fought hard to preserve all the compatibility. Unless you used export you do not need migration, existing code is fine.

I guess you really meant the question for refactoring the existing code to pick up C++11 features. Here I'd apply the general wisdom about refactoring -- never do it without a proper goal and value-based motivation.

Just that new shiny features got introduced does not impose technological debt on your code.

I suggest you start using new features in new code, and apply more liberal changes it where you refactor for different reasons anyway. And start thinking in general reshape only when having multiple styles is considered a real pain. (The multi-paradigm nature of C++ normally should allow quite much freedom, and force uniform approach only occasionally.)

From the new features what I'd focus on:

  • auto. All my new code is full of auto const and auto const& locals omitting the types. Ok, one suggested globalreplace contradicting what I said previously: replace ::iterator usage by auto if you have for loops using them.
  • lambdas, if you use algos with one-shot functions
  • the new threading stuff, especially std::future if applies to the project

If you happen to use 'std::auto_ptr' probably a good candidate for globalreplace too.

I left out move semantics because I'm yet to jump on them, not sure of the impact, so I leave it for others to suggest or not.

Upvotes: 2

justin
justin

Reputation: 104698

The compiler issues are few and easy to work through. It's far easier than adopting a new compiler. If you have the choice, stick with the std lib you use now, then update the std lib after your programs compile as C++11. You may need to stick with older versions of the library if loading dynamically.

If you want to take advantage of new features, have a look at cpp11-migrate. This tool can automate adoption of some of the new features for you when you are also ready to fully commit to c++11 (assuming your compilers support all those features).

Upvotes: 2

Baget
Baget

Reputation: 3346

Old C++ will work with your C++11 Compiler

  • Reviewing how you use iterators (maybe you can move to range-for)
  • Review if you use function pointer (maybe you can use lamdaes)
  • Review Class Initiators (maybe you can write initialization list)
  • Review your pointer use (maybe you can switch to SmartPtr)
  • Review your use to NULL with pointer maybe you can move to nullptr

Upvotes: 9

Related Questions