Re: Template question
Jeroen wrote:
John Harrison wrote:
You need to store all different types in the same list? And later you
need to recover what was the type of object stored in the list?
Sorry but in C++ that can't be done.
Oh, come on, John. There is not enough information to conclude that
it "can't be done" in C++.
V
The OP has been asking previously about a library he is writing. I
believe this is about the same code. In other words he has no control
over the types being used, the user of the library could put literally
any type on this list.
This problem cannot be solved as it is stated. Of course if the OP was
prepared to scale back his requirements he might be able to get some
workable code. I think the OP should explain exactly what he is trying
to do.
john
You are right John, this is all in the context of my previous questions.
I'm still in the phase of putting together my requirements, and thinking
of the implications for the code. While doing so I read FAQs and books
to get more knowledge, but my programming skills are rather rusty.
Fortunatly I can take my time because this is not work related :-)
Some more explanation: it was about my matrix class with some extra
facilities. Let's say you can initialize such a matrix like (OK, a
vector in this case to keep it simple...):
{
matrix m = "1 2 3 7 8";
}
But I also want to to use variables in my string expression:
{
matrix m = "1 2 3 7 8";
matrix n;
register_user_var(n);
n = "m 3 4 5 8"; // concatenate vectors 'm' and [3 4 5 8]
}
Explanation:
* register_user_var() should put variable m (in this case of type
'matrix', but it could be a double or bool, or matrix<long> as I intend
to write a template for matrix) in the list a referred to in my original
question
* when the string "m 3 4 5 8" is interpreted, the identifier 'm' is
found and looked up in the list.
So I should put in the list for each variable (this is the solution as
far as I came up with):
- pointer to the variable
- string which represents the identifier of the string
- a field that shows what type the variable is.
I thought of a macro that expands:
register_user_var(n)
to:
register_user_variable(static_cast<void *> &n, #n, typeid(n).name())
so that the underlying function automatically gets all required
parameters... All fields are stored in a structure that goed into the
list, and can be retrieved if a string is interpreted.
Maybe one of you could think of a better solution to do this. A good
pointer would do, from there I can start looking it up on the internet
or in one of my books :-)
Thanks for your time anyway,
Jeroen
The thing that strikes me is, what is the code going to look like that
interprets the list. Given the above I can't see anything except
something like this (apologies for any mistakes)
struct X
{
void* addr;
const char* name;
const char* type;
};
list<X> my_list;
// get var at front of list
void* addr = my_list.front().addr;
const char* name = my_list.front().name;
const char* type = my_list.front().type;
// do something with var
if (strcmp(type, typeid(int).name()) == 0)
{
int val = *(int*)addr;
// do something with int
}
else if (strcmp(type, typeid(double).name()) == 0)
{
double val = *(double*)addr;
// do something with double
}
etc. etc.
In other words you are limitted in the types you can put on the list by
the code you are going to write to interpret the items on the list.
If this is the case you would be much better off with something similar
to what Kai-Uwe is proposing in his last paragraph. Write a base class
that specifies the inteface that all types must implement (that
interface would specify how the type is to be interpretted when is
appears in one of your initialisation strings). Then any type that
implements the interface would be able to go on the list. This removes
the need for explicit type checking, as Kai-Uwe says.
I think the mistake you have been making upto now is thing that
templates form part of your solution, but this seems much more like a
case for traditional run-time polymorphism, i.e. base classes and
virtual functions.
john