template parameters in non-template class

S

Sebastian Faust

Hi,

I have 4 questions related to templates.

I wanna do something like the following:
template<typename T>
class Template
{
public:
Template_Test<T>()
{
j=999;
}
public:
int j;
};

class Tester
{
public:
std::list<class Template> List;

template<typename T>
void update(Template<T>& t)
{
int i = t.j;
}
};

Now my questions:
1.) In the class Tester I wanna use as the template parameter of the
std::list-class the class Template<>. I get an error at this position at
compile time ( VC.Net ). I know I could make the class Tester a class
template too and then do the following: std::list<T>, but the problem is
that I dont wanna make the Tester class a class template. So is there a
possibility how to tell the class template std::list that is should use as
template parameter a class template of Template?
2.) I made the update() member function a function template, but what I
really wanna here is only that it uses as argument the class template
Template. Is there a possibilty to do such without making update a function
template?
3.) Is it possible to use a class template as argument for a constructor,
without making the class ( in which the constructor is declared ) a class
template?
I mean something like this:
class constructor
{
constructor(Template& t) { }
};
4.) Does it make any sense to implement the observer-pattern with templates?
template<typename T>
class Subject
{
std::list<T> observers;

public:
void attach(T observer);
void detach(T observer);
};

template<typename T>
class observer
{
void update(T subject);
};

If it is possible then is my idea correct?
My problem is that at one point I wanna make a break in template using, that
means for example that the class which holds the subjects shouldnt be a
template class. But which subjects are used is only known at runtime and the
different subjects will be created at runtime, so only then it will be known
which subjects are connected with which observers. Maybe at this point using
template doesnt make any sense. Sadly I am still not that familar with
templates, so for some advice I would be very glad.

Cause I bought the book C++ templates from Josuttis and Vandevoorde, maybe
someone can tell me where in the book I can find some information about my
questions. Sadly I only read till page 60 ( I got the book some days ago,
and it's really well written ) and I wanna now use some template things in
my actual project. Sadly I couldnt find the information about my topics in
the index.

For any help thanks a lot in advance
Sebastian
 
D

David B. Held

Sebastian Faust said:
[...]
template<typename T>
class Template
{
public:
Template_Test<T>()

This doesn't make any sense. Did you mean just "Template"?
If you did, you don't need to specify the parameters. Within a
template class definition, the class name is the same as the
name with the parameters (not said well, but hopefully you get
the idea).
[...]
My problem is that at one point I wanna make a break in
template using, that means for example that the class
which holds the subjects shouldnt be a template class. But
which subjects are used is only known at runtime and the
different subjects will be created at runtime, so only then it
will be known which subjects are connected with which
observers.

In that case, you can't store the observers by value, because
you don't know what types they are. You need to store them
by reference (pointer reference, probably). But it seems
unlikely that you don't know what type they will be *at all*,
otherwise, how will you be able to call attach() and detach()?
One way is to store void*. But if you know which types *could*
get registered as observers, then you could use a variant
type, like Boost.Variant (www.boost.org). Otherwise, you
could use a list of Boost.Any.
[...]
Cause I bought the book C++ templates from Josuttis and
Vandevoorde, maybe someone can tell me where in the
book I can find some information about my questions. Sadly
I only read till page 60 ( I got the book some days ago, and
it's really well written ) and I wanna now use some template
things in my actual project. Sadly I couldnt find the information
about my topics in the index.

Heh. Well, that book probably isn't going to be useful to you
for a while. It's still worth reading right now, but you'll want some
more experience with templates before you go back to it and
try to understand the more esoteric portions. Probably a
better book to start out with from your position might be
Modern C++ Design, by Alexandrescu, although even that
might be a bit advanced. You should check out the ACCU
book review list to find other books at perhaps the intermediate
level (www.accu.org). Also, looking at real-world code that
uses templates intelligently should help you get an idea of
how they work. Browsing any of the Boost libraries will
almost surely give you an idea of real-world template usage.

Dave
 
S

Sebastian Faust

Hi,
This doesn't make any sense. Did you mean just "Template"?
If you did, you don't need to specify the parameters. Within a
template class definition, the class name is the same as the
name with the parameters (not said well, but hopefully you get
the idea).
Yes I meant just Template, sorry for that mistake. And, yes thanks I got the
idea.
In that case, you can't store the observers by value, because
you don't know what types they are. You need to store them
by reference (pointer reference, probably). But it seems
unlikely that you don't know what type they will be *at all*,
otherwise, how will you be able to call attach() and detach()?
One way is to store void*. But if you know which types *could*
get registered as observers, then you could use a variant
type, like Boost.Variant (www.boost.org). Otherwise, you
could use a list of Boost.Any.
What I first wanted to do was declare a base-class and then derivate classes
from that and use them as boost::shared_ptr in attach and detach. So I
wanted to do it almost the way as it is described in the GoF-book. What I
wanted to know is first how I can implement a break in template using, as I
described in my previous posting and second how to implement the observer
pattern with template-using.
Heh. Well, that book probably isn't going to be useful to you
for a while. It's still worth reading right now, but you'll want some
more experience with templates before you go back to it and
try to understand the more esoteric portions. Probably a
better book to start out with from your position might be
Modern C++ Design, by Alexandrescu, although even that
might be a bit advanced. You should check out the ACCU
book review list to find other books at perhaps the intermediate
level (www.accu.org). Also, looking at real-world code that
uses templates intelligently should help you get an idea of
how they work. Browsing any of the Boost libraries will
almost surely give you an idea of real-world template usage.
Why do you think that the book wont be useful for me for a while? I read the
review at ACCU and it seems to me that this book is more for "beginners"
than the the book from alexandrescu. But I decided to buy the other too, so
soon I will have both.... the only problem is then, that I still have to
read both ;), and I think that after the reading there will come tons of
testing till I understand templates better.

Bye,
Sebastian
 
D

David B. Held

Sebastian Faust said:
[...]
What I first wanted to do was declare a base-class and
then derivate classes from that and use them as
boost::shared_ptr in attach and detach.

Ah, so the types *are* related. That makes the problem
even easier, because then you can just use a base pointer.
The "OOP Way" is to use implicit derived-to-base
conversions and traffic in base pointers to get generic
behaviour. The "Generic Way" is to use templates for
unrelated types. In this case, you can go the OOP way,
and thus satisfy your constraint of not making everything
a template class. The "8-fold Way" has nothing to do with
C++, so I won't go into it here.
[...] second how to implement the observer pattern with
template-using.

Well, you might not need templates at all. Otherwise,
if you decide to try it with unrelated types, Boost.Any
might already implement what you need.
[...]
Why do you think that the book wont be useful for me for
a while?

Didn't the section on name lookups make your head spin?
The nice thing about the Vandevoorde book is that it is good
template *reference*. I'm not so sure it's a good tutorial. The
stuff they talk about is explained in more detail in other books.
What they talk about that nobody else does is the exact
detail of how templates work. If you haven't used templates
a lot to begin with, then that isn't nearly as useful as if you
have. It's like a NASA engineer telling you intricate details
about one of the Space Shuttle's fuel turbopumps without
knowing how a liquid fuel rocket engine works.
I read the review at ACCU and it seems to me that this book
is more for "beginners" than the the book from alexandrescu.
[...]

Well, if you are digesting the GoF book, then MC++D should
be ok. But the ACCU review clearly categorizes CT:TCG
as an "advanced c++" book. I honestly don't see how you
can think there is anything "beginner" about it. Anyway,
good luck!

Dave
 
S

Sebastian Faust

Hi,
Ah, so the types *are* related. That makes the problem
even easier, because then you can just use a base pointer.
The "OOP Way" is to use implicit derived-to-base
conversions and traffic in base pointers to get generic
behaviour. The "Generic Way" is to use templates for
unrelated types. In this case, you can go the OOP way,
and thus satisfy your constraint of not making everything
a template class. The "8-fold Way" has nothing to do with
C++, so I won't go into it here.
What do you think, is it really a problem making everything a template or is
this only a ghost in my mind?
Didn't the section on name lookups make your head spin?
The nice thing about the Vandevoorde book is that it is good
template *reference*. I'm not so sure it's a good tutorial. The
stuff they talk about is explained in more detail in other books.
What they talk about that nobody else does is the exact
detail of how templates work. If you haven't used templates
a lot to begin with, then that isn't nearly as useful as if you
have. It's like a NASA engineer telling you intricate details
about one of the Space Shuttle's fuel turbopumps without
knowing how a liquid fuel rocket engine works.
Yes, you are right. But as I read the review, I thought, that this book is
the more easy then the Modern C++ Design. So my thought was wrong.
I read the review at ACCU and it seems to me that this book
is more for "beginners" than the the book from alexandrescu.
[...]

Well, if you are digesting the GoF book, then MC++D should
be ok. But the ACCU review clearly categorizes CT:TCG
as an "advanced c++" book. I honestly don't see how you
can think there is anything "beginner" about it. Anyway,
good luck!
Sorry, with the "beginner" I wanted to tell that it is maybe easier than the
other book.

Btw. is it possible what I wrote in my first posting to make a constructor
template?

Bye,
Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top