# 支持 C++20 Concepts

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/clion/llms.txt).

[Concepts](https://en.cppreference.com/w/cpp/language/constraints) 是 [C++20](https://en.cppreference.com/w/cpp/20) 标准中的主要语言特性之一。 它们提供了一种对模板参数施加编译时约束的方法，帮助您确保模板符合您的预期。

> **Tip:**
> 有关 Concepts 的更多信息，请观看 [Saar Raz](https://github.com/saarraz) 在 CppCon 2019 上的演讲： [C++20 Concepts: A Day in the Life](https://youtu.be/qawSiMIXtE4)，他是 Clang 中 Concepts 实现的作者。

## 配置 Concepts 支持

Procedure: 1. 设置编译器

1. 您使用的编译器应支持 C++20 Concepts 特性。

Clang:

* 使用 Clang10 或更高版本。 您可以在 [C++ Support in Clang](https://clang.llvm.org/cxx_status.html) 中检查特定 C++20 特性的状态。

GCC:

使用 [GCC 10](https://gcc.gnu.org/gcc-10/) 或更高版本。 GCC10 中的 Concepts 实现完全 [符合 C++20](https://gcc.gnu.org/projects/cxx-status.html)。

MSVC:

MSVC 自 Visual Studio 16.3 版本起支持 Concepts。 请注意，简洁语法（  `void fn(MyConcept auto mc)` ）尚不支持。

可能需要添加  `#define __cpp_lib_concepts`  以正确解析（请参阅 [Microsoft announcement](https://devblogs.microsoft.com/cppblog/c20-concepts-are-here-in-visual-studio-2019-version-16-3/) 的评论）。

Procedure: 2. 将项目标准设置为 C++20

CMake 项目

* 当 [创建新项目](creating-new-project-from-scratch.html#create-prj)时，在新建项目向导的   语言标准   字段中选择   C++20  。

* 对于现有项目，在  `CMakeLists.txt`  的开头设置  `CMAKE_CXX_STANDARD`  变量：

```CMAKE
set(CMAKE_CXX_STANDARD 20)
```

Makefile 项目

在 Makefile 中设置  `CXXFLAGS`  变量：

```MAKE
CXXFLAGS += -std=c++20
```

Meson 项目

设置  `cpp_std`  编译器选项：

```CONSOLE
cpp_std=c++20
```

> **Tip:**
> CLion 中的 Concepts 支持在项目格式（ [CMake](quick-cmake-tutorial.html)、 [Makefiles](makefiles-support.html)、 [Meson](meson.html) 或 [compilation database](compilation-database.html)）之间的工作方式相同。 请确保将语言标准设置为 C++20 并使用适当的编译器。

Procedure: 3. 确保启用了 Clangd 补全

默认情况下，CLion 的代码补全由 [基于 Clangd 的引擎](settings-languages-cpp-clangd.html) 执行。 在使用 Concepts 时，使用默认选项   仅 Clangd 补全   或切换到   Clangd 补全与内置功能合并  。

1. 转到  `设置 | 语言与框架 | C/C++ | Clangd` 。

2. 在   代码补全   部分中，设置   通过 clangd 使用代码补全   复选框：

![Clangd 补全选项](https://resources.jetbrains.com/help/img/idea/2026.3/cl_clangd_completion.png)

## Concepts 的代码洞察

### 解析和高亮

CLion 解析并高亮显示所有标准语法形式的  `concept`  和  `requires` ：

![支持的 Concepts 语法形式](https://resources.jetbrains.com/help/img/idea/2026.3/cl_concepts_syntax_forms.png)

您可以在  `设置 | 编辑器 | 配色方案 | C/C++ | 模板 | 概念`  中调整 Concepts 的高亮设置。 默认情况下，方案继承自 C/C++   类/结构体/枚举/联合体  。

![Concepts 的语法高亮设置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_concepts_custom_highlighting.png)

### 代码分析

一组带有快速修复的 [检查](code-inspection.html) 可用于使用 Concepts 的代码。

在这些检查中，一些检查来自编译器：

![Concepts 编译器检查示例](https://resources.jetbrains.com/help/img/idea/2026.3/cl_concepts_compiler_check.png)

无约束的变量类型   检查建议对声明为  `auto`  的局部变量进行约束，如果将受约束表达式或函数调用的结果赋值给它们。 此检查在 Windows 上默认禁用。

![无约束的变量类型](https://resources.jetbrains.com/help/img/idea/2026.3/cl_concepts_constrain_inspection.png)

### 代码补全、导航和重构

Concepts 的代码补全在以下情况下可用：

* 对受约束的模板类型参数的补全： ![约束类型的补全](https://resources.jetbrains.com/help/img/idea/2026.3/cl_concepts_completion_constrained_types.png)

* 对由 `std::is_base_of<MyBase, T>` 和 `std::is_same<Other, T>` 约束的类型的补全。 `T` 在 `std::is_base_of<MyBase, T>` 和 `std::is_same<Other, T>` 中的建议列表分别包括来自 `MyBase` 和 `其他` 的选项： ![由 std::is_base_of 和 std::is_same 约束的类型的补全](https://resources.jetbrains.com/help/img/idea/2026.3/cl_concepts_completion_issame_isbase.png) 请注意，此情况下的补全仅在 `MyBase` 和 `其他` 是具体类型（而非模板类型）时有效。

[重命名](rename-refactorings.html) 重构和导航操作（如 [转到定义](navigating-through-the-source-code.html#go_to_implementation) 和 [查找用法](find-highlight-usages.html)）也支持带有 Concepts 的代码。

## See also

### 外部链接

[isocpp.org: articles tagged with 'Concepts'](https://isocpp.org/blog/tag/concepts) [CLion blog: Support for C++20's Concepts in CLion](https://blog.jetbrains.com/clion/2019/11/cpp20-concepts-in-clion/) [CppCon 2019: Saar Raz 'C++20 Concepts: A Day in the Life' (video)](https://youtu.be/qawSiMIXtE4)

### 流程

[Clangd settings](settings-languages-cpp-clangd.html) [C++ keywords completion](c-keywords-completion.html)

