如何编译Hinnant的short_alloc分配器

时间:2013-03-01 10:53:46

标签: c++ memory-management c++11 stack heap

我想尝试新的Hinnant's short_alloc allocator,据我所知,它取代旧的stack_alloc分配器。但是,我无法编译矢量示例。 g++说:

~# g++ -std=c++11 stack-allocator-test.cpp -o stack-allocator-test
In file included from stack-allocator-test.cpp:6:0:
short_alloc.h:11:13: error: ‘alignment’ is not a type
short_alloc.h:11:22: error: ISO C++ forbids declaration of ‘alignas’ with no type [-fpermissive]
short_alloc.h:11:22: error: expected ‘;’ at end of member declaration

据我所知,g++抱怨line 1011

static const std::size_t alignment = 16;
alignas(alignment) char buf_[N];

似乎编译器不喜欢alignas的“表达式版本”,但它只需要“type-id version”。

我在Ubuntu 12.10下使用g++ 4.7.2

~# g++ --version
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2

可能我错过了一些明显的东西,但我无法弄明白。任何帮助,将不胜感激。 (请不要告诉我,我必须升级到更新的g++,我太懒了,不能这样做:)

1 个答案:

答案 0 :(得分:7)

g ++ - 4.7.2不支持alignas。来自http://gcc.gnu.org/projects/cxx0x.html

  

对齐支持| N2341 | GCC 4.8

尝试使用g ++ - 4.8.0或clang;或者您也可以使用__attribute__((aligned))

__attribute__((aligned (8))) char buf_[12];

注意__attribute__((aligned))只接受某些整数常量表达式(文字,模板参数);它不接受static const变量。

相关问题