CLion 2023.2 Help

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.

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:

    CXXFLAGS += -std=c++20

    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.

    1. Go to Settings | Languages & Frameworks | C/C++ | Clangd.

    2. In the Code completion section, select either Only Clangd completion or Clangd completion merged with builtin.

      Clangd completion options

    Code insight for Concepts

    Parsing and highlighting

    CLion parses and highlights all the standard syntax forms for concept and requires:

    Supported syntax forms for Concepts

    You can tune the highlighting settings for Concepts in Settings | Editor | Color Scheme | C/C++ | Templates | Concept. By default, the scheme is inherited from C/C++ Class/struct/enum/union.

    Syntax highlighting settings for Concepts

    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:

    An example of compiler check for Concepts

    CLion provides two dedicated inspections for Concepts, Unused concept and Unconstrained variable type. You can configure them in Settings | Editor | Inspections | C/C++:

    Inspections for Concepts

    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.

    Unconstrained variable type

    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 constrained types
    • Completion for types constrained by std::is_base_of<MyBase, T> and std::is_same<Other, T>.

      The list of suggestions on T in std::is_base_of<MyBase, T> and std::is_same<Other, T> includes the options from MyBase and Other, respectively:

      Completion for types constrained by std::is_base_of and std::is_same

      Note that completion for this case works only if MyBase and Other 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.

    Last modified: 08 September 2023