Let's suppose we have the following objects:
struct A{
static const int ID = 1;
};
struct B{
static const int ID = 2;
};
struct C{
static const int ID = 3;
};
struct D{
static const int ID = 4;
};
struct Collection{
std::vector<A> o1;
std::vector<B> o2;
std::vector<C> o3;
std::vector<D> o4;
};
Collection collection;
What I want to do is getting references to some of the vector
s of the Collection
. There should be three different ways to retrieve these:
By type of
vector
, e.g.collection.get<A>();
By ID on compile time which is defined by every object that is held by a
vector
, e.g.collection.get<4>();
- By ID on runtime, e.g.
collection.get(id);
Case 1 is easy as it can be transformed into case 2 with T::ID
. Case 2 can be implemented with template specialization (although it looks ugly if I'd have lots of objects).
Case 3 is making a lot of trouble. Without some giant if
or switch
statements it seams to be nearly impossible, let alone deducing the return type.
My questions are:
- Is there a way to make case 2 more elegant?
- How should I implement case 3?