4

I'd like to try the new Hinnant's short_alloc allocator that, as far as I can understand, replaces the old stack_alloc allocator. However, I can't manage to compile the vector example. g++ says:

~# 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

As far as I can tell, g++ complains about line 10 and 11:

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

It seems that the compiler doesn't like the "expression version" of alignas but it expects just the "type-id version".

I'm using g++ 4.7.2 under Ubuntu 12.10.

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

Probably I'm missing something obvious, but I can't figure it out. Any help would be appreciated. (Please don't tell me I have to upgrade to a newer g++, I'm too lazy to do that :)

trincot
  • 317,000
  • 35
  • 244
  • 286
Avio
  • 2,700
  • 6
  • 30
  • 50
  • Yes, I thought the same, but `std::size_t` should be defined because `` is correctly included. And if it were `std::size_t`'s fault, the first error would be at `line 10`... – Avio Mar 01 '13 at 11:00
  • I'm also looking at [this answer](http://stackoverflow.com/a/11652436/1396334) to have some more clue. No luck till now. – Avio Mar 01 '13 at 11:02

1 Answers1

7

g++-4.7.2 doesn't support alignas. From http://gcc.gnu.org/projects/cxx0x.html:

Alignment support | N2341 | GCC 4.8

Try using g++-4.8.0 or clang; alternatively you may be able to use the __attribute__((aligned)):

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

Note that __attribute__((aligned)) only accepts certain integer constant expressions (literals, template parameters); it doesn't accept static const variables.

ecatmur
  • 152,476
  • 27
  • 293
  • 366