Question

This code compiles but I don't think it does what I intended, that is, move, don't copy, the boost::any object that was created on the stack into a std::vector<boost::any>

boost::any var;
var = std::string("StackOverflow");

std::vector<boost::any> vm;
vm.push_back(std::move(var));

for (auto& i : vm)
{
  std::cout << boost::any_cast<std::string>(i) << std::endl; // yes a copy exists 
}
std::cout << boost::any_cast<std::string>(var) << std::endl; // but this copy still exists too. was it not moved?? 
Was it helpful?

Solution

if you look into boost/any.hpp and observe its source code (at least find for move word) you can found that it is totally C++11 unaware (unfortunately)! so you'd better to use boost::any::swap to simulate move assign (if you still want to use boost::any at all)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top