Tips & Tricks

Support for C++20’s Concepts in CLion

Concepts are one of the biggest features coming in C++20, and knowing that, we’ve been thinking about supporting Concepts in CLion for quite a while. Enter Saar Raz with his C++20’s Concepts implementation in Clang! Long story short, we have been collaborating with Saar to merge his branch into our custom Clangd-based language engine in CLion, and started implementing some nice IDE features on top. Sounds ambitious enough, but we have thoroughly enjoyed the experience so far! A very early result of this collaboration was used by Saar in his CppCon 2019 talk on Concepts:

We do recommend watching this talk in full to learn how Concepts can be useful, and whether you should care about them even if you’re not using metaprogramming in your daily work.

Now, we are ready to present the C++20’s Concepts support in CLion, available to all the 2019.3 Early Access Program users! Just grab a build and give it a try.

DOWNLOAD CLION 2019.3 EAP

How to get C++20’s Concepts working in your project

All three major compilers are now using the corresponding support:

  • Clang
    • As we said earlier, Saar Raz is working on the implementation now. You can build the compiler from his GitHub fork and use it. It’s also available in the Compiler Explorer if you want to experiment with the compiler. Note that the Clang version is not upstreamed yet, so you’ll have to use the branch (Saar is doing his best to upstream the changes as soon as possible).
    • To use it, set the C++ standard to C++20 and pass the options -Xclang -fconcepts-ts to the compiler. In CMake, it will look as follows:
      set(CMAKE_CXX_STANDARD 20)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -fconcepts-ts")
  • GCC 10
    • For several releases, GCC supported only the Concepts-TS (under the -fconcepts flag) which is not fully consistent with C++20’s Concepts. However, in the upcoming GCC 10, the Concepts implementation was updated to conform to C++20.
    • With GCC 10, all you need to do is set the C++ standard to C++20.
  • Microsoft Visual C++ Compiler
    • MSVC has had Concepts support since Visual Studio 16.3.
    • Just set the C++ standard to C++20.
    • You may also need to add #define __cpp_lib_concepts to get the correct resolve (see the comments to the Microsoft announcement).

In CLion, you should have Clangd-based engine turned on, including code completion from Clangd (which is on by default in Settings/Preferences | Languages & Frameworks | C/C++ | Clangd).

How exactly CLion supports C++20’s Concepts

Here comes the most intriguing part – what is actually supported in CLion? There are four major areas:

  • Parsing and highlighting
  • Code inspections
  • Code completion
  • Refactoring and navigation

First of all, CLion can now parse and highlight the Concepts correctly, as we’ve merged the Saar’s Clang Concepts branch into our internal Clangd-based language engine. No more false positives are produced on code with Concepts. All the syntax forms for concept and requires from the standard are supported:
Syntax forms

The code highlighting settings for Concepts can be found and customized in Settings/Preferences | Editor | Color Scheme | C/C++ | Concept.

Several code inspections with quick-fixes are available for code using Concepts. Some checks are provided by the compiler, such as:
Compiler Concept checks

CLion gives you a dedicated Unused concept inspection.

Speaking of code completion, CLion supports a few cases:

  • Completion for template type parameters that are constrained:
    Completion for constrained type params
  • Completion for types constrained by
    • std::is_base_of<MyBase, T>
      Completion is_base
    • std::is_same<Other, T> and same_as<T, U>
      Completion same_as

Note that completion on T in std::is_base_of<MyBase, T> and std::is_same<Other, T> will offer available suggestions from MyBase and Other, respectively. But CLion does that only when possible, which means MyBase and Other have to be some concrete types. If template type is used instead, currently no completion is available.

For Concepts in this EAP build, we support the Rename refactoring and navigation actions like Go to Definition and Find Usages.

Known issues

This is still a work in progress on both ends, in CLion and in Saar’s branch, which means you might run into various issues. Hopefully, they only concern Concepts and should not affect you if you are not using C++20’s Concepts in your code.

Some known issues of the current C++20’s Concepts support in CLion:

  • Indentation does not always work properly when specifying requirements.
  • Because of Clang parser limitations, no completion can be provided for requirements specified in a constexpr if while located in a true-block.
  • Completing calls does not insert parentheses.
  • These and other CLion issues are linked to the parent ticket CPP-6584 in our tracker.
  • You may also be interested in the issues logged in Saar’s GitHub. They might affect the way CLion handles Concepts.

Give Concepts support a try in CLion and let us know what you think!

DOWNLOAD CLION 2019.3 EAP

Your CLion Team
JetBrains
The Drive to Develop

image description