C++11 个有状态分配器,带有 std::basic_string<> g++ 6.3.0



我正在尝试让 C++11 个分配器使用 std::basic_string<>。 我的代码看起来像这样(这是一个最小的示例(。 我遇到的问题是它可以在 Xcode 上运行,类似的东西在 Visual Studio 上有效,但我无法让它在 g++ 上编译。 我正在使用g ++ 6.3.0,并且尝试过-D_GLIBCXX_USE_CXX11_ABI = 1和-D_GLIBCXX_USE_CXX11_ABI = 0

#include <stdio.h>
#include <iostream>
#include <string>
template <class TYPE> class my_allocator
{
public:
int instance;
public:
using value_type = TYPE;
my_allocator(int val) : instance(val) { }
my_allocator(const my_allocator<TYPE> &other) : instance(other.instance) { }
bool operator==(const my_allocator<TYPE> &that) const { return instance == that.instance; }
bool operator!=(const my_allocator<TYPE> &that) const { return instance != that.instance; }
TYPE *allocate(const size_t number)
{
if (number == 0)
return nullptr;
if (number > (size_t)-1 / sizeof(TYPE))
throw std::bad_array_new_length();
TYPE *pointer = (TYPE *)::malloc(number * sizeof(TYPE));
if (pointer == nullptr)
throw std::bad_alloc();
return pointer;
}
void deallocate(TYPE * const pointer, size_t number) const { free(pointer); }
};
typedef std::basic_string<char, std::char_traits<char>, my_allocator<char>>my_string;
int main(void)
{
my_allocator<char> allocator(1);
my_string str(allocator);
str = "one";
std::cout << str;
return 0;
}

我不明白的是错误开始了:

In file included from /usr/include/c++/6/string:52:0,
from /usr/include/c++/6/bits/locale_classes.h:40,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from broken.cpp:6:
/usr/include/c++/6/bits/basic_string.h: In instantiation of ‘class std::basic_string<char, std::char_traits<char>, my_allocator<char> >’:
broken.cpp:49:25:   required from here
/usr/include/c++/6/bits/basic_string.h:2616:63: error: no class template named ‘rebind’ in ‘class my_allocator<char>’
typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
^~~~~~~~~~~~~~~~~

其中指出正在编译 basic_string.h 行 2616 - 但是当 basic_string.h 的第一行之一检查_GLIBCXX_USE_CXX11_ABI并且不包括构建阶段的 2616 行时,如果使用标志 -D_GLIBCXX_USE_CXX11_ABI=1 进行编译,这怎么可能呢?

我用一个 shell 脚本构建:

g++-6 -std=c++11 -Wall -D_GLIBCXX_USE_CXX11_ABI=1 broken.cpp -o broken

我正在使用的 Ubuntu 版本是:

Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:    14.04
Codename:   trusty

[编辑:此行以下的所有内容]

g++-6 -v 给出:

Using built-in specs.
COLLECT_GCC=g++-6
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 6.3.0-18ubuntu2~14.04' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=gcc4-compatible --disable-libstdcxx-dual-abi --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 6.3.0 20170519 (Ubuntu/Linaro 6.3.0-18ubuntu2~14.04) 

这是 Ubuntu 的全新安装,仅使用 sudo add-apt-repository ppa:ubuntu-toolchain-r/test 和 apt-get 安装了 cmake、g++6 和 git。 我在 Travis CI 上遇到了同样的问题,这就是为什么试图在新的 Ubuntu 安装上本地执行此操作的原因。

任何帮助,不胜感激。

分配器必须实现几件事才能在 STL 的所有上下文中使用:

您已经拥有的内容:

  • value_type指定分配的对象类型
  • allocatedeallocate
  • ==!=检查通过一个分配器分配的存储是否可以通过另一个分配器解除分配。
  • 复制构造函数

隐式生成的内容:

  • 复制赋值运算符
  • 移动
  • 构造函数、移动赋值运算符

缺少什么:

  • 重新绑定构造函数:类似

    template<typename OTHER_TYPE>
    my_allocator(const my_allocator<OTHER_TYPE> &other) ...
    

    允许 STL 使用自定义节点类型来存储元素,而您只为元素类型提供了分配器,例如在set或类似的数据结构中

如果提供了重新绑定构造函数并且模板参数足够"简单",则rebind结构本身是可选的(请参阅分配器概念和std::allocator_traits(

最新更新