Avio
Avio

Reputation: 2717

How to compile Hinnant's short_alloc allocator

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 :)

Upvotes: 4

Views: 627

Answers (1)

ecatmur
ecatmur

Reputation: 157464

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.

Upvotes: 7

Related Questions