Blog 3 min read

Easily detect where you can modernize your C++ codebase.

Share this article
Easily detect where you can modernize your C++ codebase.

C++11/C++14/C++17 include several additions to the core language and extend the C++ standard library. Some of these new features are very easy to use and bring significant value to your C++ projects.

It can be useful to automatically detect places where some of the new C++11 features can be used. For this purpose, clang-tidy is a standalone tool used to automatically convert C++ code written according to older standards to use features of the newest C++ standard where appropriate.

Developers who use Clang can easily take advantage of the clang-tidy tool. However, for Visual C++ developers and users of other compilers, it is not as easy to benefit from the results of this useful tool.

To enable all C++ developers to benefit from this tool, CppDepend now integrates it into its Windows 2018.1 release to detect 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 such as for(…; …; …) that can be replaced with the new range-based loops introduced in C++11, and provide the corresponding range-based loop expression.
  • Pass-By-Value: Detect const-ref parameters that would benefit from using the pass-by-value idiom.
  • auto_ptr: Detect uses of the deprecated std::auto_ptr and replace them with std::unique_ptr.
  • auto specifier: Detect places where the auto type specifier can be used 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 inherited from the C library were deprecated in C++ and are no longer recommended 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 a new expression, and replaces it with a call to std::make_shared.
  • std::unique_ptr: This check finds the creation of std::unique_ptr objects by explicitly calling the constructor and a new expression, and replaces it with a call to std::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; you can enable it from “Project properties” => Analysis => Modernize C++ Code. However, enabling it will increase the time required to analyze your codebase, so we recommend disabling it once you no longer need the results.

After the analysis, you can find the CQLinq queries containing the C++11 recommendations in the “Modernize C++ Code” group:

For each query, you can see the places where your code can be modernized. You can double-click each result to go to the source code location where the improvement can be applied.

What’s particularly useful about the recommendations provided by the Clang modernizer tool is that they cover some of the most useful C++11 features, which are easy to introduce into your source code and can add significant value to your C++ projects.

Share this article