templated members in non-template classes

This is a discussion on templated members in non-template classes within the C++ Programming forums, part of the General Programming Boards category; Given a class with a templated member: Code: template<typename T> class tmpl_class {}; class some_class { tmpl_class<T>& tmpl_member; public: template<typename ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    templated members in non-template classes

    Given a class with a templated member:
    Code:
    template<typename T> class tmpl_class {};
    
    class some_class
    {
      tmpl_class<T>& tmpl_member;
    public:
      template<typename T>
      some_class( tmpl_class<T>& oc )
        : tmpl_member(oc)
      {}
    };
    I can't tell if this is valid code (no compiler atm). I'm sure there has to be a way to have a non-template class have templated members, but I haven't figured it out yet. In fact, I just came up with the idea above right now, even though I've been trying to figure this out for a week. Sorry if it's offensively incorrect.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Why are you trying to do this exactly? If you want to have templated members, then you need to declare the class with a template. Unless you're trying to do the following:

    Code:
    template<typename T> class tmpl_class {};
    
    class some_class
    {
      tmpl_class<std::string>& tmpl_member;
    public:
      some_class( tmpl_class<std::string>& oc )
        : tmpl_member(oc)
      {}
    };
    Which more or less defeats the purpose of tmpl_class.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    In code, I'd do something like this:
    Code:
    void f()
    {
      tmpl_class<std::string> service; // std::string is just an example
      some_class handle(service);
    }
    I simply want some_class to work with any type of service, and for the code to show that (ie. some_class shouldn't be templated). The thing is, if I were using inheritance from an ABC rather than a templated class, doing this would be simple. I'd essentially be using the bridge pattern. Surely there's a way to transpose that idea over to templates?

    A more direct question would be: can a non-template class have a member that is itself a template, whose template parameter depends on what the class is constructed with?

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    can a non-template class have a member that is itself a template, whose template parameter depends on what the class is constructed with?
    No, because the constructor is called at run time but the template argument type needs to be known at compile time to build a template for compilation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 09:44 AM
  2. Accessing members of a class within a template
    By chadwickstein in forum C++ Programming
    Replies: 10
    Last Post: 10-03-2006, 10:47 AM
  3. Templated classes...
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2006, 05:31 PM
  4. Templated singleton classes
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2006, 05:06 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21