Results 1 to 4 of 4

Thread: templates in a class

  1. #1

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197

    templates in a class

    How would I make a template function for a class? (I mean a member function that is a template. Not an entire template class) Im getting parse errors from having the 'template <typename T>' thing above the prototype and member function....

    Prototype and member function code please
    Last edited by markman; Sep 29th, 2002 at 07:06 PM.
    retired member. Thanks for everything

  2. #2
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Hi,
    Stroustrup prefers 'class' over 'typename' because it is shorter and quicker to type. See "The C++ Programmers Language", Third Edition, page 858 to read it for yourself.

    I read from one source that 'typename' was not supported by all compilers. I don't understand nor can I explain this.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    template <class T>
    T mymin(T v1, T v2)
    {
      return ((v1<v2)?v1:v2);
    }
    
    int main(int argc, char *argv[])
    {
      int i=123;
      int j=456;
      int min= mymin(i,j);
      cout << min << endl;
      system("pause"); //I use Dev-C++...need this here.
      return 0;
    }
    Regards,
    ChuckB

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Chuck:

    typename was introduced only later, as class might be confusing to newbies. It is never a good thing to have one keyword mean two different thigns in different contextes.
    Your fingers won't fall off from typing 3 characters more. Neither will Bjarne's.

    Since typename wasn't there from the beginning some older compilers don't support it.

    As to you code, I think he asked for member template functions. I don't see why this shouldn't work:

    Code:
    class A {
    public:
      template <typename/class T>
      void SomeFunc(T arg);
    };
    
    template <class/typename T>
    void A::SomeFunc(T arg) {
    // blabla
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    CornedBee,
    I'm still trying to get this terminology down. I understand now the expression 'member function'. I do like the 'class/typename' usage. I haven't tried that.
    I admit, I was confused using the word class in my function example.

    You know, I'm just glad I have a clue what this thread was saying. :-)

    Regards,
    ChuckB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width