I'm trying to get C++11 allocators working with std::basic_string<>. My code looks something like this (this is a minimal example). The problem I'm having is that it works on Xcode, and something similar works on Visual Studio, but I can't get it to compile on g++. I'm using g++ 6.3.0 and I've tried with -D_GLIBCXX_USE_CXX11_ABI=1 and -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;
}
What I don't understand is that the error starts:
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;
^~~~~~~~~~~~~~~~~
Which states that basic_string.h line 2616 is being compiled - but how can that be if compiled with the flag -D_GLIBCXX_USE_CXX11_ABI=1 when one of the first lines of basic_string.h checks _GLIBCXX_USE_CXX11_ABI and does not include line 2616 in the build phase if its true?
I build with a shell script:
g++-6 -std=c++11 -Wall -D_GLIBCXX_USE_CXX11_ABI=1 broken.cpp -o broken
The version of Ubuntu I'm using is:
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
[EDIT: Everything below this line]
g++-6 -v gives:
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)
This is a fresh install of Ubuntu with only cmake, g++6 and git installed using sudo add-apt-repository ppa:ubuntu-toolchain-r/test and apt-get. I get the same problem on Travis CI which is why'm trying to do it locally on a fresh Ubuntu install.
Any help is appreciated.