I have a large code base that can use boost::any or boost::spirit::hold_any (depending on a macro definition).
hold_any
seems to be compatible with boost::any
(e.g. How to print boost::any to a stream? or Type erasure - Part IV) and faster (Why you shouldn’t use boost::any) but I'm experiencing several segmentation fault errors using hold_any
(Boost v1.55 / 1.54 / 1.53).
This is a minimal working example that exhibits the same problem as the original code:
#include <iostream>
#include <string>
#include <vector>
#include <boost/spirit/home/support/detail/hold_any.hpp>
typedef boost::spirit::hold_any any;
typedef std::vector<any> vany;
int main()
{
vany data0, data1;
for (unsigned i(0); i < 1000; ++i)
{
std::string s("test_test_test");
data0.push_back(any(s));
}
const unsigned n(data0.size());
vany::iterator iter(data0.begin());
for (unsigned i(0); i < n; ++i)
{
std::cout << "Moving " << i << std::endl;
data1.push_back(*iter);
iter = data0.erase(iter);
}
return 0;
}
The program appears to work correctly:
- changing from
boost::spirit::hold_any
toboost::any
; - changing the content of the
hold_any
to a data type small enough to perform small buffer optimization (e.g. fromstd::string
toint
).
It seems strange that there could be some major bug in a widely used library such as Boost Spirit, but
- I'm having a hard time finding a bug in the example;
- I've tried g++ / clang++ without success.
What's wrong with the example?