简体   繁体   English

将c ++ 98代码升级到c ++ 11

[英]Upgrading c++98 code to c++11

I have inherited a large c++ code base and have finally got it to compile and run on 64-bit Red Hat 7 (gcc version 4.8.5 20150623). 我继承了一个大的c ++代码库,最终得到它在64位Red Hat 7(gcc版本4.8.5 20150623)上编译和运行。

By default the makefiles in the project are compiling for c++98. 默认情况下,项目中的makefile正在为c ++ 98进行编译。 I would like to turn on c++11 as that obviously provides many language benefits. 我想打开c ++ 11,因为这显然提供了许多语言优势。 g++ 4.8 has rather complete support for c++11. g ++ 4.8对c ++ 11有相当完整的支持

My question is: What kind of risk does this introduce? 我的问题是:这会带来什么样的风险? What things do I need to watch out for? 我需要注意什么? What type of things could break (besides things just not compiling)? 什么类型的东西可能会破坏(除了不编译的东西)?

My google-foo is rather low on this because I've searched and just haven't been able to find any kind of analysis on this. 我的google-foo相当低,因为我已经搜索过,但却未能找到任何类型的分析。 (Maybe because their really isn't any risk, I just don't know.) (也许是因为他们真的没有任何风险,我只是不知道。)

Just turning on -std=c++11 and nothing else should be fairly low risk. 只需打开-std=c++11 ,其他任何东西都应该是相当低的风险。 You may see some small speed improvements due to moves suddenly being performed where they previously couldn't be, but I doubt you'll run into any problems (unless the code contains undefined behaviour and gcc exploits that more agressively in C++11 mode - I don't know if it does). 您可能会看到一些小的速度改进,因为突然执行它们以前不能执行的操作,但我怀疑您会遇到任何问题(除非代码包含未定义的行为,并且gcc在C ++ 11模式下更加激进) - 我不知道是不是。

As for modernizing the code you may want to look at clang tidy/clang modernize. 至于代码的现代化,你可能想看看clang tidy / clang modernize。 But once you start to actually actively replace old working code with new modern C++11 versions, that is where you need to be really careful and make sure you got it right and fully understand both the code you are replacing as well as the new feature(s) you use to replace it. 但是,一旦你开始用新的现代C ++ 11版本实际上主动替换旧的工作代码,那么你需要非常小心并确保你正确地完全理解你要替换的代码以及新的代码。您用来替换它的功能。

http://clang.llvm.org/extra/clang-tidy/index.html http://clang.llvm.org/extra/clang-tidy/index.html

See especially the 'modernize-*' checks: http://clang.llvm.org/extra/clang-tidy/checks/list.html 特别参见'modernize- *'检查: http//clang.llvm.org/extra/clang-tidy/checks/list.html

What kind of risk does this introduce? 这引入了什么样的风险? What things do I need to watch out for? 我需要注意什么?

Not much usually, the current standard ( ) seamlessly compiles older standards code. 通常情况下,当前标准( )无缝编译旧标准代码。

What type of things could break (besides things just not compiling)? 什么类型的东西可能会破坏(除了不编译的东西)?

You may see some warnings about deprecated stuff, like eg std::auto_ptr but it will still work correctly though. 您可能会看到一些关于已弃用的东西的警告,例如std::auto_ptr但它仍然可以正常工作。


You have to judge, if it's actually worth to refactor your older code, or just leave it as is. 你必须判断,实际上是否值得重构你的旧代码,或者只是保持原样。

If there may be improvement for interfaces using newer language features, you mey consider refactoring. 如果使用较新语言功能的接口可能有所改进,您可以考虑重构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM