1

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?? 
user841550
  • 1,067
  • 3
  • 16
  • 25

1 Answers1

3

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)

zaufi
  • 6,811
  • 26
  • 34
  • I checked the source 1.53 and didn't see any evidence but boost codes tends to be beyond me so thought I am missing something. But then I saw this discussion https://svn.boost.org/trac/boost/ticket/6999 – user841550 Aug 10 '13 at 19:58
  • @user841550 there is nothing to miss ;) -- it is ~254 lines long and ~6k in size :). you ought to trust your eyes more :) (and believe in self) – zaufi Aug 10 '13 at 20:14