template parameters in non-template class

Discussion in 'C++' started by Sebastian Faust, Oct 17, 2003.

  1. 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
     
    Sebastian Faust, Oct 17, 2003
    #1
    1. Advertising

  2. "Sebastian Faust" <> wrote in message
    news:bmpuri$cj7$04$-online.com...
    > [...]
    > 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



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
     
    David B. Held, Oct 18, 2003
    #2
    1. Advertising

  3. 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
     
    Sebastian Faust, Oct 18, 2003
    #3
  4. "Sebastian Faust" <> wrote in message
    news:bmro8o$ek$02$-online.com...
    > [...]
    > 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



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
     
    David B. Held, Oct 18, 2003
    #4
  5. 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
     
    Sebastian Faust, Oct 18, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Graham
    Replies:
    0
    Views:
    568
    Matt Graham
    Jul 21, 2003
  2. BigMan
    Replies:
    3
    Views:
    1,447
    BigMan
    Apr 22, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    769
    Pete Becker
    May 4, 2005
  4. nguillot
    Replies:
    5
    Views:
    539
  5. kito
    Replies:
    2
    Views:
    425
Loading...

Share This Page