0

Why are we suppose to use template parameters at front of every function even if we are not using deduced template parameters in the function. As we can see that i am not using template parameter _T in printP() function (around 30) then why it is required to include template syntax at front of this function.

NOTE: This is very simplified version of my big class, and it might look silly because it is very small but, consider a situation where you are using template for only few [2-3] function of your class but you are bound to type (even copy past) this lengthy template syntax at front of every function but i am asking why??.

Is there any way to get of this

#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

template<typename _T>
class Thing{
        int _p;
        _T _k;
    public:
        Thing(int p, _T k):_p(p),_k(k){}
        void printK();
        void printP();
    };

template<typename _T>
void Thing<_T>::printK(){
    cout << _k << endl;
    }

template<typename _T>
void Thing<_T>::printP(){
    cout << _p << endl;     // as we can see that i am not using template paramerter "_T" 
    }                       // here in this function then why it is required to include template syntax

int main()
{
    Thing<int> a(1,2);
    a.printK();
    a.printP();
}
Rupesh Yadav.
  • 894
  • 2
  • 10
  • 24
  • When you define a member function out-of-line, you need to qualify it with the class name. And the class name is `Thing<_T>`, not `Thing` (which is the name of a template). – Kerrek SB Nov 20 '14 at 10:50
  • This is a very annoying thing in C++. This is why I don't bother to separate my functions outside of a templated class unless absolutely necessary. It's just not worth the hassle. – Neil Kirk Nov 20 '14 at 10:52
  • but there need to be something, because i dont want to put my function definitions in my class, and i cant see my function look this much ugly :( :( :( – Rupesh Yadav. Nov 20 '14 at 10:55
  • 1
    Get over it. You're not allowed to name your template parameters `_T` anyway (it's a [reserved name](http://stackoverflow.com/a/228797/981959)), so it should be `Thing` which is a bit less ugly. – Jonathan Wakely Nov 20 '14 at 11:12
  • oo yee @JonathanWakely , but by seeing standard headers now i m habituated :) – Rupesh Yadav. Nov 20 '14 at 11:14
  • **NO! Stop it!** The rules are there especially to protect standard headers from users and vice versa, don't break the rules. Your code is not a standard header, don't use names only allowed in standard headers. – Jonathan Wakely Nov 20 '14 at 11:16
  • you @JonathanWakely are absolutely Mr Right :). i will keep in mind thanks. – Rupesh Yadav. Nov 20 '14 at 11:17

2 Answers2

1

Because the function PrintK is member of template class Thing<_T>. For a member function definition outside the class, the function name also includes class name(to which it belongs, here it belongs to Thing), since Thing is template, so function name requires you to provide template parameter (T here). e.g. Function definition outside class requires the following syntax **

return type class name:: function name (argument list)

* Here class (Thing) is template class, so its name will also require type (like Thing<_T>). I hope you got my point.

Ali Kazmi
  • 1,460
  • 9
  • 22
0

Its usually a good idea to restrict the members and functions of a template class to those that are dependent on the template parameters. Non-dependent members and functions can be put in a separate non=template class (is there a better name?). For example:

#include <iostream>

using namespace std;

class ThingBase
{
public:
    ThingBase(int p)
        : _p(p)
    {
    }
    void printP();
protected:
    int _p;
};

void ThingBase::printP(){
    cout << _p << endl;
}


template<typename _T>
class Thing : public ThingBase {
    _T _k;
public:
    Thing(int p, _T k)
        : ThingBase(p),
          _k(k){}
    void printK();
};

template<typename _T>
void Thing<_T>::printK(){
    cout << _k << endl;
}


int main()
{
    Thing<int> a(1,2);
    a.printK();
    a.printP();
}