【问题标题】:How to compile Hinnant's short_alloc allocator如何编译 Hinnant 的 short_alloc 分配器
【发布时间】:2013-02-15 20:27:13
【问题描述】:

我想试试新的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 的“表达式版本”,但它只需要“类型 ID 版本”。

我在 Ubuntu 12.10 下使用 g++ 4.7.2

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

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

【问题讨论】:

  • 是的,我也是这么想的,但是应该定义std::size_t,因为正确包含<cstddef>。如果是std::size_t 的错,第一个错误将在line 10...
  • 我也在查看this answer 以获得更多线索。到目前为止还没有运气。

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


【解决方案1】:

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 变量。

【讨论】:

  • 好吧,我怀疑。谢谢你的链接。
  • 你是对的。使用__attribute__((aligned (8))) 编译时不会出错。再次感谢您。
猜你喜欢
  • 2013-10-14
  • 2015-04-02
  • 2016-02-16
  • 2019-06-02
  • 2019-12-13
  • 2012-07-23
  • 2016-02-08
  • 2018-04-10
  • 2015-08-11
相关资源
最近更新 更多