How to link existing C ++ code with C ++ 11

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

How to act? Are both the same or is there a compiler dependency?

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

In general, what changes are needed, if any?

+6
source share
4 answers

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

  • Overview of using iterators (maybe you can go to the range)
  • See if you are using a function pointer (maybe you can use lamdaes)
  • Overview of class initiators (perhaps you can write an initialization list)
  • Check out using a pointer (maybe you can switch to SmartPtr)
  • View your usage in NULL with a pointer, maybe you can go to nullptr
+9
source

Problems with the compiler are few and easy to use. This is much simpler than adopting a new compiler. If you have a choice, stick with the std lib that you are using now, and then upgrade the std lib after your programs compile as C ++ 11. You may need to stick to older versions of the library when loading dynamically.

If you want to use the new features, see cpp11-migrate . This tool can automate the adoption of some new functions for you when you are also ready to fully execute C ++ 11 (assuming that your compilers support all these functions).

+2
source

Migration? I thought WG21 fought hard to maintain all compatibility. If you did not use export, you do not need migration, the existing code is in order.

I think you really had in mind the issue of reorganizing existing code to get C ++ 11 features. Here I would apply the general wisdom regarding refactoring - never do it without the right goal and motivation based on value.

Just new brilliant features have appeared, does not impose technological debt on your code.

I suggest you start using the new features in the new code and apply more liberal changes where you refactor for various reasons. And to start thinking altogether, change only when having several styles is considered a real pain. (The nature of a C ++ multi-paradigm usually should provide enough freedom and a uniform approach only occasionally).

Of the new features that I would focus on:

  • auto. All my new code is populated with the auto const and auto const& locales, omitting the types. Well, one suggested a globalreplace, contrary to what I said earlier: replace :: using an auto iterator, if you have to use their loops.
  • lambdas if you use algos with single shot features.
  • new stream, especially std::future , if applied to the project

If you use 'std :: auto_ptr', then probably a good candidate for globalreplace too.

I left the semantics of movement because I still need to jump on them, not sure about the impact, so I leave it to others to suggest or not.

+2
source

As others have pointed out, your code is likely to be compiled. If you're wondering what might go wrong, then check out

What hacked changes are made in C ++ 11?

If you plan to change your old code to take advantage of C ++ 11, I would add auto to Baget's answer .

+2
source

Source: https://habr.com/ru/post/946335/


All Articles