Thread: template parameter on a function that dont use the template

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    template parameter on a function that dont use the template

    Hi,

    I just discovered this today, I wonder if this is standard enough?

    Code:
    class Boohwa{
       public:
          void mooya(){
             cout << "boohwa!" << endl;
          }
    };
    
    class What{
       public:
          
          template<class M>
          void mooya(){
             M moo;
             moo.mooya();
          }
          
          
    };
    and later, you can do:

    Code:
       What what;
       what.mooya<Boohwa>();
    Would love to hear where I can read more about this stuff, I was just experimenting around. This is potentially SUPER useful.. I'd imagine?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That code is standard (as in it isn't against any C++ rules).

    This is potentially SUPER useful.. I'd imagine?
    Well its only useful if it solves some problem better than any other method

    If you want to expand on your idea a little more, you could always do something like:
    Code:
    class Boohwa{
       public:
          virtual void mooya(){
             cout << "boohwa!" << endl;
          }
    };
    
    template <class M>
    class What : public M
    {
    };
    
    // ...
    
    What<Boohwa> what;
    what.mooya();
    Templates can be pretty interesting since they can be very powerful. It can be easy to get yourself into trouble with very convoluted code if you're not careful though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. More explicit template member function hell
    By SevenThunders in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 10:36 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM