Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must I declare template above each function in a template?

Tags:

c++

templates

I'm writing a template, and I'm a bit new to doing that. I've noticed that every tutorial I run into declares the template above each function that is implemented. I'm curious if this is for a reason, if there is a way around it, and how it is useful.

Example: Within some file (we will call it template.h):

template <class T> class MyClass
{
public:
    MyClass(T parameter);
    T getParameter() const;
    void setParameter();
    void doSomethingUseless() const;
private:
    T mParameter;
}


template<class T>
MyClass<T>::MyClass(T parameter):mParameter{parameter}
    {}

template<class T>
T MyClass<T>::getParameter() const
    {
        return mParameter;
    }

template<class T>
void MyClass<T>::setParameter(T parameter)
    {
        mParameter = parameter;
    }
template<class T>
void MyClass<T>::doSomethingUseless() const
    {
        for (int i = 0; i < 10000; i++)
    }
  • Why must every function be templated if it has nothing to do with the template type? (for example, the doSomethingUseless method)
  • Why are both the template<class T> and the MyClass<T> required above each function for this to compile?
  • Am I approaching this incorrectly/Is there an easier way to do this?

Anyways, thanks for looking. As a note, I did not compile the example above, I generalized a different template class that I wrote (without a bunch of busy functions in there) If there are mistakes, I apologize.

like image 705
Michael Smith Avatar asked Jun 23 '16 19:06

Michael Smith


People also ask

What is template What is the need of template declare a template class?

Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach to programming where generic types are used as parameters in algorithms to work for a variety of data types.In C++, a template is a straightforward yet effective tool.

What is the importance of function templates?

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

Why do templates need to be in header?

To have all the information available, current compilers tend to require that a template must be fully defined whenever it is used. That includes all of its member functions and all template functions called from those. Consequently, template writers tend to place template definition in header files.

How do you declare a template function?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>( float original ); Template arguments may be omitted when the compiler can infer them.


1 Answers

The member functions of a template class are themselves also templates. Because of this, they need to be defined with any required template parameters (in your case T). If you did not do this, you might write a function like:

void MyClass::memberFunc(T var) {}

What is T in this case? There's no T defined within this scope.

So what do people do? Many code bases will simply define their functions inline with the class. However others insist on separating them, like you have done. The unfortunate reality is that it's simply a requirement of the language.

like image 175
Sam Cristall Avatar answered Oct 12 '22 16:10

Sam Cristall