Boost bimap 无法使用 gcc 10、c++20 进行编译。寻找临时修复

2023-12-05

使用 gcc 10.1 和 boost 1.73.0,以下代码

#include <boost/bimap.hpp>

int main() {
  boost::bimap<int, int> lookup;
}

无法使用标志进行编译-O2 --std=c++20,但会成功并带有标志-O2 -std=c++17(使用编译器资源管理器验证)。

这可能与以下问题有关:https://github.com/boostorg/bimap/pull/15(已弃用std::allocator<void>)

我现在可以使用一些解决方法来成功编译此代码吗--std=c++20?


错误背后的原因并不是std::allocator<void>专业化是removed。实际上,它作为主要模板仍然有效void争论。代码无法编译,因为::rebind不再属于std::allocator本身。相反,实现应该使用std::allocator_traits<Alloc>::rebind_alloc<U>.

幸运的是,大多数容器通常允许指定自定义分配器作为模板参数。对于以下情况也是如此boost::bimap,其中分配器可以是第三个、第四个或第五个模板参数,根据docs。它默认为std::allocator, 但反而,boost::container::allocator可以使用,不会产生错误:

#include <boost/bimap.hpp>
#include <boost/container/allocator.hpp>

int main() {
  boost::bimap<int, int, boost::container::allocator<int>> lookup;
}

This issue最近已修复6fba6e5. boost::bimap现在可以正确检测嵌套::rebind可用:

template<class A, class T, class = void>
struct allocator_rebind {
    typedef typename detail::alloc_to<A, T>::type type;
};

template<class A, class T>
struct allocator_rebind<A, T,
    typename detail::alloc_void<typename A::template rebind<T>::other>::type> {
    typedef typename A::template rebind<T>::other type;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Boost bimap 无法使用 gcc 10、c++20 进行编译。寻找临时修复 的相关文章

随机推荐