2014-12-24 48 views
3

我有以下簡單的測試代碼。使用boost :: pool_allocator時不會調用移動構造函數

Move constructor 
Move constructor 
Copy constructor 
Copy constructor 

爲什麼使用boost::pool_allocator阻止使用移動的構造函數,編譯器:

#include <stack> 
#include <iostream> 
#include "boost/pool/pool_alloc.hpp" 

struct Frame 
{ 
    uint32_t i{}; 

    Frame(uint32_t _i) : i(_i) {} 

    Frame(const Frame& f) 
    { 
     std::cout << "Copy constructor" << std::endl; 
     i = f.i; 
    } 

    Frame(Frame&& f) 
    { 
     std::cout << "Move constructor" << std::endl; 
     std::swap(i, f.i); 
    } 
}; 

int main(int argc, char* argv[]) 
{ 
    { 
     std::stack<Frame, std::deque<Frame>> stack; 

     Frame f(0); 
     stack.push(std::move(f)); // Move constructor 
     stack.push(Frame(1)); // Move constructor 
    } 

    { 
     std::stack<Frame, std::deque<Frame, boost::pool_allocator<Frame>>> stack; 

     Frame f(0); 
     stack.push(std::move(f)); // Copy constructor 
     stack.push(Frame(1)); // Copy constructor 
    } 


    return 0; 
} 

當我編譯這段代碼要麼鐺或GCC,它給了我下面的輸出?
我錯過了什麼嗎?

回答

5

pool_allocator不完全向前的參數construct:它只是需要一個const參考值類型並傳遞作爲初始化放置new

這是因爲pool_allocator尚未更新爲C++ 11。

相關問題