[英]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(在这种情况下,您无需重新绑定),也不要继承自字符串,这些类都不打算用作公共基础。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.