Support for C++20 Concepts
Concepts are one of the major language features of the C++20 standard. They provide a way to put compile-time constraints on template arguments, helping you ensure the templates meet your expectations.
CLion's implementation of Concepts support is based on the Clangd engine, and it is currently a work in progress on both CLion's and Clang's ends. You can track the status and leave your feedback in the issues linked to CPP-6584.
Configure Concepts support
1. Set up the compiler
The compiler you are using should support the C++20 Concepts feature.
Use Clang10 or later. You can check the status of a particular C++20 feature in C++ Support in Clang.
Use GCC 10 or later. The implementation of Concepts in GCC10 fully conforms to C++20.
MSVC supports Concepts since Visual Studio version 16.3. Note that terse syntax (void fn(MyConcept auto mc)
) is not supported yet.
Adding #define __cpp_lib_concepts
might be required for correct resolve (see the comments to the Microsoft announcement).
2. Set the project standard to C++20
CMake project
When creating a new project, select C++20 in the Language standard field of the New Project wizard.
For an existing project, set the
CMAKE_CXX_STANDARD
variable at the beginning of CMakeLists.txt:set(CMAKE_CXX_STANDARD 20)
Makefile project
Set the CXXFLAGS
variable in the Makefile:
3. Make sure Clangd completion is enabled
By default, CLion's code completion is performed by the Clangd-based engine. When working with Concepts, use the default option Only Clangd completion or switch to Clangd completion merged with builtin.
Go to
.In the Code completion section, select either Only Clangd completion or Clangd completion merged with builtin.
Code insight for Concepts
Parsing and highlighting
CLion parses and highlights all the standard syntax forms for concept
and requires
:
You can tune the highlighting settings for Concepts in Class/struct/enum/union.
. By default, the scheme is inherited from C/C++Code analysis
A set of inspections with quick-fixed is available for your code using Concepts.
Among the inspections, some checks come from the compiler:
CLion provides two dedicated inspections for Concepts, Unused concept and Unconstrained variable type. You can configure them in :
Concepts are also covered by all the relevant Unused code inspections such as Unused include directive.
The Unconstrained variable type inspection suggests constraining local variables declared as auto
if the result of a constrained expression or function call is assigned to them. This inspection is disabled by default on Windows and is not supported for concepts declared with the requires
clause syntax.
Code completion, navigation, and refactoring
Code completion for Concepts is available in the following cases:
Completion for template type parameters that are constrained:
Completion for types constrained by
std::is_base_of<MyBase, T>
andstd::is_same<Other, T>
.The list of suggestions on
T
instd::is_base_of<MyBase, T>
andstd::is_same<Other, T>
includes the options fromMyBase
andOther
, respectively:Note that completion for this case works only if
MyBase
andOther
are concrete types (not template types).
Rename refactoring and navigation actions like Go to Definition and Find Usages are also supported for code with Concepts.