473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Template question

Hi all,

I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;
};

In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.

My question: what does the list look like in C++? I'm lost, so if anyone
has some advice....

Thanks,

Jeroen
Mar 6 '07 #1
16 3115
Jeroen wrote:
Hi all,

I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;
};

In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.

My question: what does the list look like in C++? I'm lost, so if anyone
has some advice....

Thanks,

Jeroen
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.

john
Mar 6 '07 #2
Jeroen wrote:
I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;
};

In a list,
What's the definition of your 'list'?
I'd like to store pointers to objects of class A.
There is no "class A" until you define what the template argument is.
There is only "class template A".
But I
don't know at forehand if these will be A<intor A<doubleor
whatever (all these flavours of A must be stored in the same list).
There is no such thing as "flavour" in C++. Here you talk about what
is known as "instantiations" of the same template. They are _different_
*types*. Objects of different types cannot be stored in the same
container (unless you define your container in some very tricky and
special way). So, what's the definition of your 'list'?
And, lateron if I retrieve the objects from the list I may have to
know what flavour the object was in order to call the proper routines
for processing them.
My question: what does the list look like in C++? I'm lost, so if
anyone has some advice....
What problem are you trying to solve? It is very likely that you
need a "heterogeneous container". Look it up. But it's impossible
to advise anything given such vague "problem definition".

Also read the FAQ. What you seem to describe here is usually solved
through polymorphism (and it's not all that difficult). But you did
not say what kind of "processing" you are going to do, nor does your
class template 'A' have any functionality, which begs the question,
why don't you just store 'ints' and 'doubles' in your 'list'?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask`
Mar 6 '07 #3
John Harrison wrote:
Jeroen wrote:
>Hi all,

I have a question which is illustrated by the following piece of
code: template <class T>
class A {
T my_value;
};

In a list, I'd like to store pointers to objects of class A. But I
don't know at forehand if these will be A<intor A<doubleor
whatever (all these flavours of A must be stored in the same list).
And, lateron if I retrieve the objects from the list I may have to
know what flavour the object was in order to call the proper
routines for processing them. My question: what does the list look like
in C++? I'm lost, so if
anyone has some advice....

Thanks,

Jeroen

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 6 '07 #4
>>
>>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
Mar 6 '07 #5
Jeroen wrote:
Hi all,

I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;
};

In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.
[snip]

First, A<intand A<doubleare _unrelated_ types. Thus, A<int>* and
A<double>* are not mutually convertible into one another. That effectively
prevents putting declaring a std::list<what_goes_herethat would contain
A<int>* and A<double>* objects at the same time.

One way to deal with this is:

class A_base {};

template < typename T >
class A : public A_base {
...
};

Now, you could have a std::list<A_base*>. In order to test the dynamic type
at run-time, you could use something like this:

template < typename T >
bool has_A_type ( A_base * ptr ) {
return ( dynamic_cast< A<T>* >( ptr ) != 0 );
}

I am writing this from memory without looking at the standard. So there
might be a problem. However, I am pretty certain that dynamic_cast can be
used some way or another to test for the dynamic type.

Now,

if ( has_A_type<int>( ptr ) ) {
// process int
}
if ( has_A_type<double>( ptr ) ) {
// process int
}
...

will be possible. _However_, it will not be good design.
You should consider putting virtual functions into A_base with overrides in
A<T>. Those will resolve automatically to the correct version for the type.
Ideally that should remove the necessity of testing for the dynamic type.

Best

Kai-Uwe Bux

Mar 6 '07 #6
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<longas 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
Mar 6 '07 #7
Kai-Uwe Bux wrote:
>Jeroen wrote:
>>Hi all,

I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;
};

In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.

[snip]

First, A<intand A<doubleare _unrelated_ types. Thus, A<int>* and
A<double>* are not mutually convertible into one another. That effectively
prevents putting declaring a std::list<what_goes_herethat would contain
A<int>* and A<double>* objects at the same time.

One way to deal with this is:

class A_base {};

template < typename T >
class A : public A_base {
...
};

Now, you could have a std::list<A_base*>. In order to test the dynamic type
at run-time, you could use something like this:

template < typename T >
bool has_A_type ( A_base * ptr ) {
return ( dynamic_cast< A<T>* >( ptr ) != 0 );
}

I am writing this from memory without looking at the standard. So there
might be a problem. However, I am pretty certain that dynamic_cast can be
used some way or another to test for the dynamic type.

Now,

if ( has_A_type<int>( ptr ) ) {
// process int
}
if ( has_A_type<double>( ptr ) ) {
// process int
}
...

will be possible. _However_, it will not be good design.
You should consider putting virtual functions into A_base with overrides in
A<T>. Those will resolve automatically to the correct version for the type.
Ideally that should remove the necessity of testing for the dynamic type.

Best

Kai-Uwe Bux


OK, I'll take some time to look at this and see if I can synchronize
this with what I want :-) Thanks for thinking with me,

Jeroen
Mar 6 '07 #8
Jeroen wrote:
[..]
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";
Note, that "m" here is only known to the compiler. There is no "m"
during run-time. Just so we're clear...
}

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]
Now, here the first symbol in the string literal is just a value of
the 'char' object. The compiler has no way to associate it with the
object you named 'm' in your program. The interpretation of the string
literal is delayed until the run-time, and then the object's name 'm'
doesn't exist any more (unless you provide some mechanism to keep the
name around, that is).
}

Explanation:

* register_user_var() should put variable m (in this case of type
'matrix', but it could be a double or bool, or matrix<longas I
intend to write a template for matrix) in the list a referred to in
my original question
Uh... So, what I suppose we should be seeing here is

register_user_var(m, "m");

probably, right?
* when the string "m 3 4 5 8" is interpreted, the identifier 'm' is
found and looked up in the list.
OK.. So, you're writing an expression interpreter, eh? Why didn't
you say so before?
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())
Aha...

Don't use 'typeid(n).name()', it's utterly unreliable. If you need to
store the type, store 'std::typeinfo' itself ('typeid(n)'), not 'name()'.

But then the biggest question here is what do you do with the 'name'
(or 'typeinfo') afterwards. What kind of cast to you propose to use
to regain access to the object (via a pointer to it) and the object's
inner things? It's very easy to do (void*)&n, but then you have this
'p' of type 'void*', and the name, and 'typeinfo', then what? I can
see how the name is used to look 'p' up. And what do you do with it?
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.
Right. Retrieved. And then what?
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 :-)
The Chapter 6 in Stroustrup's TC++PL describes an expression interpreter.
Check it out.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 6 '07 #9
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<longas 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<Xmy_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
Mar 6 '07 #10
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.
Tell that to the author of boost::any.
Mar 6 '07 #11
Noah Roberts 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.


Tell that to the author of boost::any.
Boost::any does not allow you to 'recover what was the type of object
stored'. Sure you can say, was it an int? was it a double? etc. But in
the absense of any clue (like a fixed list of possibilities) you cannot
tell what the type was.

john
Mar 6 '07 #12
On 3ÔÂ7ÈÕ, ÉÏÎç2ʱ43·Ö, Jeroen <no_m...@please.com>wrote:
Hi all,

I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;

};

In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.

My question: what does the list look like in C++? I'm lost, so if anyone
has some advice....

Thanks,

Jeroen
I suggest use the boost::variant.
On other way , you can do like this:
Plan A..
struct Type_Base
{
virtual std::string type_name() = 0;
virtual void * adress() = 0;
};

template <typename T>
class MyType : Type_Base
{
public:
std::string type_name() { return type_id(T).name(); }
void * address(){ return (void *)(&value);}
private:
T value;
}

Anyway boost::variant is better then paln A,but it only support
arithmetic type. You can read the document for detail.

Mar 7 '07 #13
Jeroen schreef:
John Harrison wrote:
OK guys, you all gave me a lot of pointers to think about. It will keep
me busy for some time to sort everything out. Thanks again!
Mar 7 '07 #14

Jeroen wrote:
>
I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;
};

In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.

My question: what does the list look like in C++? I'm lost, so if anyone
has some advice....
Design pattern "adapter" can be used to adapt templated class into virtual
base interface.

class Base
{
public:
virtual void method()=0;
virtual ~Base(){}
};

template<class T>
class A_Base: public Base
{
A<T data;

public:
void method(){ data.method(); }
};

Multiple ingeritance also can be used

template<class T>
class A_Base: public Base, public A<T>
{
public:
void method(){ A<T>::method(); }
};

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Mar 9 '07 #15
On Mar 7, 3:43 am, Jeroen <no_m...@please.comwrote:
Hi all,

I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;

};

In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.

My question: what does the list look like in C++? I'm lost, so if anyone
has some advice....

Thanks,

Jeroen
As a basic technique, you just overload the functions with different
possible types.Compiler will do call the appropriate one.
You can also use typeid operator to identify the type. but it will
make some overhead to your program as it enables Runtime Type
Identification. (RTTI). typeid operator returns a type_info class
which can be use for comparison, type string etc.
You can also approach boost type traits library

Mar 9 '07 #16
On Mar 6, 8:43 pm, Jeroen <no_m...@please.comwrote:
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]

}
Hopefully I am not wildly off base here, but it looks like you're
getting yourself in a lot of trouble trying to initialise a matrix via
a string.
Better is something like this:
Matrix<intim(3,3); // 3x3 matrix of ints
im = 1,2,3,
4,5,6,
0,9,8;

This can be achieved by having Matrix<T>::operator=(T) return an
initialiser object - call it Minit<T>.
Then you define Minit<T>::operator,(T) to put T into the Matrix at
the next "slot", and return itself.
I have working code if you want me to post it, this technique is
nabbed from "Generative Programming" by Eisenecker and Czarnecki.

Mar 9 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
1
by: Leslaw Bieniasz | last post by:
Hello, I have the following problem: file a.h --------------- template <class T> class A { // some stuff
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
4
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.