Opened 7 years ago
#11970 new Feature Requests
Use perfect forwarding for object_pool
Reported by: | Owned by: | Chris Newbold | |
---|---|---|---|
Milestone: | To Be Determined | Component: | pool |
Version: | Boost 1.61.0 | Severity: | Problem |
Keywords: | object_pool | Cc: |
Description
boost::object_pool::construct uses a code generation system to create versions of that method that take up to some arbitrary number of input parameters. With C++11/14 it is possible to do the same using perfect forwarding, which would allow any number of input parameters using a single template method, and without bloating the hpp.
I think this would also solve a related problem I am having. The object that I am trying to construct using the object_pool takes a unique_ptr as an input parameter to it's constructor (it is taking ownership of it):
class Node {} class Tree { Tree(std::unique_ptr<Node> root, int foo, int bar) : m_root(std::move(root)) {} private: std::unique_ptr<Node> m_root; } void makeTrees() { boost::object_pool<Tree> treePool; std::unique_ptr<Node> node (new Node()); Tree* tree = treePool.construct (node, 1, 2); }
In the current implementation, the compiler gives an error because object_pool::construct doesn't call std::move on root when newing the tree:
/usr/local/include/boost/pool/detail/pool_construct.ipp(258): error: function "std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp> &) [with _Tp=Node, _Dp=std::function<void (Node *)>]" (declared at line 273 of "/usr/include/c++/4.8.5/bits/unique_ptr.h") cannot be referenced -- it is a deleted function try { new (ret) element_type(a0, a1, a2); }
I think (but am not certain) that using perfect forwarding would allow this.