带有 clang++ v4 和 gcc 6.3 库的自定义分配器
Custom allocators with clang++ v4 and gcc 6.3 libraries
在库附带 gcc 6.3.1 的系统(arch linux(上使用带有 clang++ 4.0.0 的自定义分配器时,我遇到了很多麻烦。 这是一个最小的非工作示例:
#include <string>
struct myalloc : std::allocator<char> {
using std::allocator<char>::allocator;
};
struct mystring
: std::basic_string<char, std::char_traits<char>, myalloc> {
using std::basic_string<char, std::char_traits<char>, myalloc>::basic_string;
};
int
main()
{
mystring r = "hello";
mystring s (std::move(r));
}
我在这里的目的显然是让myalloc
成为一个行为与系统std::allocator
完全相同的自定义分配器,并且mystring
与std::string
相同,只是它使用myalloc
。 这应该是最不可能导致问题的情况。 (显然,一旦这起作用,我想进一步自定义分配器。
代码使用 g++ -std=c++14 -Wall -Werror
进行干净编译,但clang++ -std=c++14
失败并显示:
In file included from strerror.cc:1:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/string:52:
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/bits/basic_string.h:477:9: error:
no matching constructor for initialization of
'std::__cxx11::basic_string<char, std::char_traits<char>,
myalloc>::_Alloc_hider'
: _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strerror.cc:7:8: note: in instantiation of member function
'std::__cxx11::basic_string<char, std::char_traits<char>,
myalloc>::basic_string' requested here
struct mystring
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/bits/basic_string.h:109:2: note:
candidate constructor not viable: no known conversion from 'typename
std::remove_reference<allocator<char> &>::type' (aka
'std::allocator<char>') to 'const myalloc' for 2nd argument
_Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/bits/basic_string.h:107:14: note:
candidate constructor (the implicit move constructor) not viable: requires
1 argument, but 2 were provided
struct _Alloc_hider : allocator_type // TODO check __is_final
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/bits/basic_string.h:107:14: note:
candidate constructor (the implicit copy constructor) not viable: requires
1 argument, but 2 were provided
1 error generated.
这只是 clang 或 gcc 库中的错误,还是我的代码在概念上存在问题?
最小
示例的最小修复是将此成员添加到struct myalloc
template<class> struct rebind {
using other = myalloc;
};
当然,最好不要从 std::allocator 继承(在这种情况下,你不需要重新绑定(,也不要从字符串继承,这些类不打算作为公共基础。
相关文章:
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- C++自定义分配器大小参数作为模板参数会引发编译器错误
- c++ 中的自定义分配器,用于不调用secure_string实现
- 我可以对 std::array 使用自定义分配器来获取安全加密密钥吗?
- shared_ptr的删除程序是否存储在自定义分配器分配的内存中?
- 自定义分配器,包括放置新案例
- 对 std::regex_match 使用自定义分配器
- 为什么我的代码不适用于自定义分配器?
- 为什么这个自定义分配器的析构函数在 GCC/MSVS 的 stdlib 中被调用两次
- std::cout 如果从自定义分配器 (Visual Studio 2019) 调用,则不会输出
- 通过自定义分配器和删除器有效使用shared_ptr
- 如何将带有自定义分配器的 std::vector 传递给期望带有 std::分配器的函数?
- 自定义分配器兼容性
- 使用自定义分配器及其替代项重载基本类型
- C 中STL地图的自定义分配器
- 带有 clang++ v4 和 gcc 6.3 库的自定义分配器
- 不调用带有 std::vector 的自定义分配器
- 何时使用自定义分配器
- 为什么当我使用自定义分配器溢出向量时没有出现分段错误?
- 如何将自定义分配器的完全相同状态传递给多个容器?