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

yoy*_*yoy 4 c++ boost c++20 gcc10

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

#include <boost/bimap.hpp>

int main() {
  boost::bimap<int, int> lookup;
}
Run Code Online (Sandbox Code Playgroud)

使用 flags 编译失败-O2 --std=c++20,但使用 flags会成功-O2 -std=c++17(使用编译器资源管理器验证)。

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

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

Pio*_*cki 8

错误背后的原因不是std::allocator<void>专业化被删除。实际上,它作为带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;
}
Run Code Online (Sandbox Code Playgroud)

问题最近已在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;
};
Run Code Online (Sandbox Code Playgroud)