关于Boost Program Options使用C++已弃用特性的疑问
问题描述
通过vcpkg安装了Boost Program Options 1.78版本,使用clang++并以-std=c++20编译时出现如下警告,但使用g++编译时无此问题。根据C标准定义,std::unary_function自C11起已被弃用。
In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/program_options/variables_map.hpp:12: In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/any.hpp:20: In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/type_index.hpp:29: In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/type_index/stl_type_index.hpp:47: /home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:132:33: warning: 'unary_function<const std::error_category *, unsigned long>' is deprecated [-Wdeprecated-declarations] struct hash_base : std::unary_function<T, std::size_t> {}; ^ /home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:692:18: note: in instantiation of template class 'boost::hash_detail::hash_base<const std::error_category *>' requested here : public boost::hash_detail::hash_base<T*> ^ /home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:420:24: note: in instantiation of template class 'boost::hash<const std::error_category *>' requested here boost::hash<T> hasher; ^ /home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:551:9: note: in instantiation of function template specialization 'boost::hash_combine<const std::error_category *>' requested here hash_combine(seed, &v.category()); ^ /bin/../lib/gcc/x86_64-redhat-linux/12/../../../../include/c++/12/bits/stl_function.h:124:7: note: 'unary_function<const std::error_category *, unsigned long>' has been explicitly marked deprecated here } _GLIBCXX11_DEPRECATED; ^ /bin/../lib/gcc/x86_64-redhat-linux/12/../../../../include/c++/12/x86_64-redhat-linux/bits/c++config.h:2340:32: note: expanded from macro '_GLIBCXX11_DEPRECATED' # define _GLIBCXX11_DEPRECATED _GLIBCXX_DEPRECATED ^ /bin/../lib/gcc/x86_64-redhat-linux/12/../../../../include/c++/12/x86_64-redhat-linux/bits/c++config.h:2331:46: note: expanded from macro '_GLIBCXX_DEPRECATED' # define _GLIBCXX_DEPRECATED __attribute__ ((__deprecated__))
请问:
- 为何Boost会使用标准库中的已弃用部分?
- 忽略这些警告并通过
-Wno-deprecated-declarations抑制是否存在问题?
解答
1. Boost使用已弃用std::unary_function的原因
- Boost 1.78是2021年发布的版本,为了兼容C++03及更早版本的代码,保留了对
std::unary_function的继承。在C++03中,这个模板用来提供argument_type和result_type类型别名,早期Boost组件的泛型逻辑依赖这些别名来保证跨版本兼容性。 - clang与g的警告触发逻辑存在差异:clang在C11及更高标准下会默认触发
std::unary_function的弃用警告,而你使用的GCC 12版本可能默认未开启这类警告,或者对标准库弃用标记的检测规则更宽松,因此没有输出警告。
2. 抑制警告的安全性
- 短期来看完全安全:
std::unary_function只是被标记为弃用,并未从你当前使用的libstdc++(GCC 12的标准库)中移除,代码可以正常编译运行。 - 添加
-Wno-deprecated-declarations只是消除干扰性警告,不会影响程序功能。如果想要彻底解决这个问题,更推荐升级到Boost 1.80及以上版本——这些版本已经移除了对std::unary_function的依赖,完全适配C++11及以后的标准。
内容的提问来源于stack exchange,提问作者David Carpenter
相关产品推荐
相关产品推荐

