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-tidy is a standalone tool used to automatically convert C++ code, written against old standards, to use features of the newest C++ standard where appropriate.
Developers who utilze Clang could easilly use the clang-tidy tool. However for Visual C++ developers and other compilers users it’s not an easy task to benefit from this interesting tool result.
To let all C++ developers benefit from this amazing tool, CppDepend is integrating it now in its latest windows version 2018.1 in order 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 use this feature?
This feature is not enabled by default, and you can enable it from “Project properties”=> Analysis=>Modernize C++ Code.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.
After the analysis you can find the CQLinq queries concerning the C++11 advices inside the “Modernize C++ Code” group:
And for each query, you can get the places where you can modernize your code. You can double click each result to go in your source code where the improvements could be applied.
What’s interesting in the advises given by the Clang modernizer tool is the fact that they concern some of the most interesting C++11 features which are very easy to introduce in your source code and bring a big added value to your C++ projects.