问题描述
使用gcc 10.1和boost 1.73.0,以下代码
With gcc 10.1 and boost 1.73.0, the following code
#include <boost/bimap.hpp>
int main() {
boost::bimap<int, int> lookup;
}
无法使用标志 -O2 --std = c ++ 20
进行编译,但是会成功使用标志 -O2 -std = c ++ 17
(已通过验证编译器资源管理器).
fails to compile with flags -O2 --std=c++20
, but will succeed with flags -O2 -std=c++17
(verified with compiler explorer).
这可能与以下问题有关: https://github.com/boostorg/bimap/pull/15 (不推荐使用 std :: allocator< void>
)
This is possibly related to the following issue: https://github.com/boostorg/bimap/pull/15 (deprecated std::allocator<void>
)
我现在可以使用一些解决方法来使此代码成功地通过-std = c ++ 20
进行编译吗?
Is there some workaround I can use for now to get this code to successfully compile with --std=c++20
?
推荐答案
该错误的原因并非是 std :: allocator< void> 专业化已被删除 .实际上,它作为带有 void
参数的主要模板仍然有效.代码无法编译,因为 :: rebind
不再是 std :: allocator
本身的一部分.相反,实现应使用 std :: allocator_traits< Alloc> ::: rebind_alloc< U>
.
The reason behind the error is not that the std::allocator<void>
specialization is removed. Actually, it's still valid as the primary template with the void
argument. The code fails to compile, because ::rebind
is no longer part of std::allocator
itself. Instead, an implementation should use std::allocator_traits<Alloc>::rebind_alloc<U>
.
幸运的是,大多数容器通常允许将自定义分配器指定为模板参数.根据文档.它默认为 std :: allocator
,但是可以使用 boost :: container :: allocator
,它不会产生错误:
Fortunately, most containers usually allow to specify a custom allocator as a template argument. It's also the case for boost::bimap
, where an allocator can be the third, fourth or fifth template parameter, according to docs. It defaults to std::allocator
, but instead, boost::container::allocator
can be used, which does not produce the error:
#include <boost/bimap.hpp>
#include <boost/container/allocator.hpp>
int main() {
boost::bimap<int, int, boost::container::allocator<int>> lookup;
}
此问题最近已在 6fba6e5 . boost :: bimap
现在可以正确检测嵌套的 :: rebind
是否可用:
This issue has recently been fixed in 6fba6e5. boost::bimap
now properly detects if a nested ::rebind
is available:
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;
};
这篇关于Boost bimap无法使用gcc 10,c ++ 20进行编译.寻找临时解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!