Boost1.75搭配GCC11使用bimap出现concept check警告不升级Boost如何修复
问题根因
你遇到的concept failed类警告属于Boost版本与编译器的兼容性误报:Boost 1.75的概念检查实现中存在大量仅用于类型校验、不会被实际使用的局部typedef,GCC 11默认开启了未使用局部typedef的诊断规则,因此编译时会输出大量这类告警,告警本身不影响代码的正常运行逻辑和执行结果。
可行解决方案(无需升级Boost版本)
- 方案1:全局禁用Boost概念检查
在引入所有Boost头文件之前,定义BOOST_DISABLE_CONCEPTS宏即可,修改后的代码示例如下:
#define BOOST_DISABLE_CONCEPTS #include <boost/bimap.hpp> #include <string> #include <iostream> int main() { typedef boost::bimap<std::string, int> bimap; bimap animals; animals.insert({"cat", 4}); animals.insert({"shark", 0}); animals.insert({"spider", 8}); std::cout << animals.left.count("cat") << '\n'; std::cout << animals.right.count(8) << '\n'; }
- 方案2:添加GCC编译参数抑制告警
如果你不想禁用Boost概念检查,可以在编译参数中加入-Wno-unused-local-typedefs,直接屏蔽该类未使用局部typedef的告警,原有代码无需修改。 - 方案3:局部屏蔽Boost头文件的告警
如果只想屏蔽当前文件中Boost头文件触发的告警,不影响全局编译规则,可以用GCC诊断指令包裹Boost头文件引入部分:
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <boost/bimap.hpp> #pragma GCC diagnostic pop #include <string> #include <iostream> int main() { typedef boost::bimap<std::string, int> bimap; bimap animals; animals.insert({"cat", 4}); animals.insert({"shark", 0}); animals.insert({"spider", 8}); std::cout << animals.left.count("cat") << '\n'; std::cout << animals.right.count(8) << '\n'; }
以上三种方案均可以完全消除该场景下的concept failed类警告,且不会影响原有代码的运行逻辑和结果。
内容的提问来源于stack exchange,提问作者Saad Rami
相关产品推荐
相关产品推荐

