【问题标题】:why g++ doesn't support functional with allocator constructor on Ubuntu?为什么 g++ 不支持 Ubuntu 上的分配器构造函数?
【发布时间】:2016-06-13 05:40:06
【问题描述】:
#include <vector>
#include <functional>

int main(int argc, char** argc)
{
    std::vector<int> x;
    std::function<void (void)> f(std::allocator_arg,x.get_allocator());
    return 1;
}

使用:g++ test.cpp -o test -std=c++11
它在Ubuntu 15,我的g++ version is 5.2.1 上失败了。 XCodeVS2015 一切正常。我检查了/usr/include/c++/5/functional,它没有分配器的构造函数。我检查了www.cplusplus.com,它用分配器定义了构造函数。
有人可以告诉我如何解决这个问题,或者我必须更改 g++ stl, 如何用其他 stl 更改 g++ stl?我已经下载了 sgi stl 源代码。
请帮忙 !非常感谢。

【问题讨论】:

  • allocator_arg 是一个常量 - 你的代码没有比std::function&lt;void (void)&gt; f(PI); 更有意义。
  • 直到 GCC 6.1.0,std::function with allocator 仍然不被支持,来源有评论// TODO: needs allocator_arg_t
  • 非常感谢。我放弃使用分配器了~

标签: c++ c++11 ubuntu stl


【解决方案1】:

std::function中使用分配器有很多问题。

What's the point of std::function constructor with custom allocator but no other args?

http://cplusplus.github.io/LWG/lwg-closed.html#2386

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0302r0.html

查看g++的functional源码,它显然没有使用任何分配器construct进行动态分配,它明明使用newdelete

所以,我不确定如何提供分配器来进行堆分配。但是,一个坏主意是从function 派生并重载newdelete 运算符。我建议你走这条路。这是一个非常非常糟糕的想法。

【讨论】:

    猜你喜欢
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 2013-12-22
    • 1970-01-01
    • 2011-05-23
    相关资源
    最近更新 更多