S
Simon Elliott
I'd like to do something along these lines:
struct foo
{
int i1_;
int i2_;
};
struct bar
{
double d1_;
double d2_;
};
typedef std::vector<foo> vectorFoo;
typedef std::vector<bar> vectorBar;
union vectorAll
{
vectorFoo foo_;
vectorBar bar_;
};
static vectorAll myVectorAll;
....
foo myFoo;
myVectorAll.foo_.push_back(myFoo);
myVectorAll.foo_.clear();
....
bar myBar;
myVectorAll.bar_.push_back(myBar);
myVectorAll.bar_.clear();
But a) I'm fairly sure that this isn't valid C++ because std::vector
must have a constructor, b) it seems very unsafe. and c) it precludes
sensible requirements such as clearing myVectorAll without knowing what
type is in it. (Intuitively one might want to be able to do a
myVectorAll.clear());
Given that one of my target compilers is too uncompliant to compile
boost, I can't use boost::any for this. What are my other options?
struct foo
{
int i1_;
int i2_;
};
struct bar
{
double d1_;
double d2_;
};
typedef std::vector<foo> vectorFoo;
typedef std::vector<bar> vectorBar;
union vectorAll
{
vectorFoo foo_;
vectorBar bar_;
};
static vectorAll myVectorAll;
....
foo myFoo;
myVectorAll.foo_.push_back(myFoo);
myVectorAll.foo_.clear();
....
bar myBar;
myVectorAll.bar_.push_back(myBar);
myVectorAll.bar_.clear();
But a) I'm fairly sure that this isn't valid C++ because std::vector
must have a constructor, b) it seems very unsafe. and c) it precludes
sensible requirements such as clearing myVectorAll without knowing what
type is in it. (Intuitively one might want to be able to do a
myVectorAll.clear());
Given that one of my target compilers is too uncompliant to compile
boost, I can't use boost::any for this. What are my other options?