I am trying to use boost::any to encapsulate the sqlite return values. I then tried to write a loop to print these.
My first thought was to do something like:
for(boost::any field: row) {
switch(field.type()) {
case typeid(double):
double value = any_cast<double>(field);
...
break;
case typeid(other type):
...
}
}
Now for the experienced programmer it becomes obvious that this can not work since typeid returns an instance rather than a numeric id.
After some research I figured I might try either typeid(...).hash_code()
however this is not sufficiently constexpr
qualified (Beside the danger of hash collision).
Questions
- Is there a better way than building an excessive
if ... else ...
labyrinth to handle objects based on their typeid? - Is there a reason why
hash_code
is not aconst_expr
? Is this a result of the separate compilation of object files? - What is the use of
std::type_index
? Considering that it only provides some additional operators (<
,<=
,>
,>=
) why was it not possible to integrate its functionality withstd::type_info
?
I have a feeling you are looking for boost variant and static visitations.
Since variants weren't mentioned, this might be worth posting as an answer. Demonstruction:
Live On Coliru
Prints