mercredi 24 décembre 2014

Move constructor is not called when using boost::pool_allocator

I have the following simple test code.



#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;
}


When i compile this code with either clang or gcc, it gives me the following output:



Move constructor
Move constructor
Copy constructor
Copy constructor


So my question is: Why does using boost::pool_allocator prevent the compiler from using the move constructor? Am i missing something?


Aucun commentaire:

Enregistrer un commentaire