2016-06-13 72 views
2
#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源代碼。
請幫忙!非常感謝 。爲什麼g ++在Ubuntu上不支持allocator構造函數的功能?

+0

'allocator_arg'是一個常數 - 你的代碼是沒有更多的意義比'的std ::函數 F(PI);'。 –

+1

直到GCC 6.1.0,帶分配器的'std :: function'仍然不被支持,源代碼有評論'// TODO:needs allocator_arg_t' – Mine

+0

非常感謝。我放棄使用allocator〜 – NeoLiu

回答

2

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運營商。我會建議你不要沿着這條道路走下去。這是一個非常非常非常有用的想法。

相關問題