如何阻止YCM在C++20模板中显示红色错误提示?
问题说明
错误截图

待排查代码
#include <concepts> #include <iostream> using namespace std; template <std::integral T> void print(T i) { std::cout << "Integral: " << i << '\n'; } int main() { print (1); }
当前YCM相关Vundle配置
Plugin 'ycm-core/YouCompleteMe' let g:ycm_clangd_binary_path = '/usr/bin/clangd' let g:ycm_global_ycm_extra_conf = '/home/hlajungo/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'
解决方法
问题根源是clangd默认未启用C20标准,无法识别std::integral这类C20特性,两种解决方式任选其一:
方式1:修改全局YCM配置文件
打开指定的全局配置文件/home/hlajungo/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py,找到flags数组,添加-std=c++20编译选项:
flags = [ # 保留原有其他编译选项 '-std=c++20', # 若标准库路径存在识别问题,可添加对应系统路径(示例为gcc 11的路径,根据实际调整) '-isystem', '/usr/include/c++/11', '-isystem', '/usr/include/x86_64-linux-gnu/c++/11', ]
方式2:在Vim中为C++文件单独配置
在~/.vimrc中添加自动命令,让所有C文件默认使用C20标准:
autocmd FileType cpp let b:ycm_flags = ['-std=c++20']
修改完成后重启Vim,打开目标代码文件,YCM的红色错误标记即可消除。
内容的提问来源于stack exchange,提问作者趴蒽翔
相关产品推荐
相关产品推荐

