Modernize your C++ code
C++11/C++14/C++17 includes several additions to the core language and extends the C++ standard library. Some of these new features are very easy to use and bring a big added value to your C++ projects.
It's interesting to detect automatically places where we can use some C++11 new features. For such needs clang-modernize is a standalone tool used to automatically convert C++ code, written against old standards, to use features of the newest C++ standard where appropriate.
The CppDepend windows version integrates now the clang-modernize tool to detect the places where the following features could be used:
- Override: Detect places where you can add the override specifier to member functions that override a virtual function in a base class and that don’t already have the specifier
- Loop Convert: Detect loops like for(…; …; …) to replace them with the new range-based loops in C++11 and give you the new range loop expression to use.
- Pass-By-Value: Detect const-ref parameters that would benefit from using the pass-by-value idiom.
- auto_ptr: Detect the uses of the deprecated std::auto_ptr to replace by std::unique_ptr.
- auto specifier: Detect places where to use the auto type specifier in variable declarations.
- nullptr: Detect null literals to be replaced by nullptr where applicable.
- std::bind: The check finds uses of
std::bind
and replaces simple uses with lambdas. Lambdas will use value-capture where required. - Deprecated headers: Some headers from C library were deprecated in C++ and are no longer welcome in C++ codebases. Some have no effect in C++. For more details refer to the C++ 14 Standard [depr.c.headers] section.
- std::shared_ptr: This check finds the creation of
std::shared_ptr
objects by explicitly calling the constructor and anew
expression, and replaces it with a call tostd::make_shared
. - std::unique_ptr: This check finds the creation of
std::unique_ptr
objects by explicitly calling the constructor and anew
expression, and replaces it with a call tostd::make_unique
, introduced in C++14. - raw string literals: This check selectively replaces string literals containing escaped characters with raw string literals.
How do I enable this feature?
This feature is not enabled by default, and you can enable it from "Project properties"=> Plugins=>Enable Clang-Tidy Analysis. However, it will take more time to analyse your code base and it's recommended to disable it as soon as you no longer need its results.
![modernization configuration in cppdepend](/Img/modernizeconfig.png)
Once the analysis is complete, you'll be able to locate the CQLinq queries related to C++11 recommendations within the "Modernize C++ Code" category:
![modernization queries in cppdepend](/Img/modernizerules.png)
For every query, you can identify areas where your code could be modernized. By double-clicking on each result, you'll be navigated directly to the relevant section in your source code where potential enhancements can be implemented.
![modernization sample in cppdepend](/Img/modernizesample.png)