Results 1 to 4 of 4

Thread: What's the reason for mentioning the <T> in the definition of a template member

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default What's the reason for mentioning the <T> in the definition of a template member

    Why is the syntax of a template member definition written in the following way -
    Qt Code:
    1. template <typename T>
    2. MyList<T>::push_back(T & value) { ... }
    To copy to clipboard, switch view to plain text mode 
    and not
    Qt Code:
    1. template <typename T>
    2. MyList::push_back(T & value) { ... } // why can't it be MyList:: instead of MyList<T>::
    To copy to clipboard, switch view to plain text mode 
    Is there any reason for the usage of <T> immediately after the class name?
    Thanks.
    Last edited by rawfool; 20th November 2017 at 17:54.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: What's the reason for mentioning the <T> in the definition of a template member

    Well, what happens if you want to add a method that takes another type, T1 for example?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: What's the reason for mentioning the <T> in the definition of a template member

    Could you give me an example, please.
    Did you mean this?

    Qt Code:
    1. template <typename T>
    2. class MyList
    3. {
    4. public:
    5. void push_back(const T & t);
    6. template <typename T1>
    7. void emplace_back(const T1 & t);
    8. };
    9.  
    10. template <typename T>
    11. void MyList<T>::push_back(T const & t)
    12. {
    13. cout << t << endl;
    14. }
    15.  
    16. template <typename T>
    17. template <typename T1>
    18. void MyList<T>::emplace_back(const T1 & t) // Still in this case we are preceding with "template <type T>", but I don't understand the need of <T> in MyList<T>::
    19. {
    20. cout << "This is another type " << t << endl;
    21. }
    22.  
    23. int main()
    24. {
    25. MyList<string> obj;
    26. obj.push_back(string("rahul"));
    27. obj.emplace_back(int(23));
    28. }
    To copy to clipboard, switch view to plain text mode 

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: What's the reason for mentioning the <T> in the definition of a template member

    yes, that is what I meant.
    When you define a calss template, you need to specify the template type when us initialize the template.
    You could get rid of the class template deceleration and have a non template class, which has only template functions, then you don't need to specify the template type on the class level.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    rawfool (21st November 2017)

Similar Threads

  1. Replies: 2
    Last Post: 10th October 2014, 09:49
  2. pass member function as argument int template function
    By bibhukalyana in forum General Programming
    Replies: 1
    Last Post: 12th March 2013, 07:05
  3. Replies: 2
    Last Post: 28th March 2012, 21:47
  4. Replies: 2
    Last Post: 20th October 2011, 15:14
  5. Replies: 3
    Last Post: 19th February 2008, 13:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.