boost::any:
I tried to compile and run the following code to test this:
#include <boost/any.hpp>
#include <boost/pool/object_pool.hpp>
int main()
{
boost::object_pool<boost::any> pool;
boost::any *i = pool.malloc();
*i = 1;
boost::any *j = pool.construct(2);
pool.destroy(i);
pool.destroy(j);
}
But it gets a segfault in the boost::any destructor.
boost::variant:
Trying to compile and run the following:
#include <boost/any.hpp>
#include <boost/pool/object_pool.hpp>
#include <boost/variant.hpp>
int main()
{
typedef boost::variant<int, double> my_variant;
boost::object_pool<my_variant> pool;
my_variant *i = pool.malloc();
*i = 1;
my_variant *j = pool.construct(2);
pool.destroy(i);
pool.destroy(j);
}
And I got the following error:
a.out: visitation_impl.hpp:207: typename Visitor::result_type boost::detail::variant::visitation_impl(int, int, Visitor&, VPCV, mpl_::true_, NBF, W*, S*) [with W = mpl_::int_<20>; S = boost::detail::variant::visitation_impl_step, boost::mpl::l_iter >; Visitor = boost::detail::variant::invoke_visitor
; VPCV = void*; NBF = boost::variant::has_fallback_type_; typename Visitor::result_type = bool; mpl_::true_ = mpl_::bool_]: Assertion `false' failed. Aborted (core dumped)
Is this expected behavior? Does the boost::pool only work for simple C++ types like int, doble, float, etc?
Yes, boost pool works with both. You're simply using it wrong.
malloc
only allocates uninitialized memory. It's your error to expect to be able to assign to it as if it were a fully functional instance of the object type that the point implies.This fixes it:
Live On Coliru
This also runs clean under asan/ubsan and valgrind.
Bonus Question
For POD types or trivial types you could get away with eliding the constructor, much like the C++ compiler is allowed to elide them in those cases.