Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between member functions for a template class defined inside and outside of the class

Is there a difference between defining member functions for a template class inside the class declaration versus outside?

Defined inside:

template <typename T>
class A
{
public:
    void method()
    {
        //...
    }
};

Defined outside:

template <typename T>
class B
{
public:
    void method();
};

template <typename T>
void B<T>::method()
{
    //...
}

For non-template classes, this is the difference between inlined and non-inlined methods. Is this also true for template classes?

The default for most of my colleagues is to provide definitions inside the class, but I've always preferred definitions outside the class. Is my preference justified?

Edit: Please assume all the above code is provided in the header file for the class.

like image 705
Ben Avatar asked Jan 20 '10 17:01

Ben


People also ask

What is the difference between member function inside the class or outside the class?

A member function is declared in the class but defined outside the class and is called using the object of the class. A non-member function that is declared outside the class but called a normal function inside the main function.

What is the difference between member function defined inside and outside the body of the class how is inline member function defined outside the body of a class?

A member function can be defined inside the class body where it is declared. Function's entire body is defined inside class body. A member function can be defined outside the class. To define a function outside the class, scope resolution operator is used.

How are member functions defined inside and outside the class explain with examples?

In this example, the member function putdata() is defined inside the class book. Hence, putdata() is by default an inline function. Note that the functions defined outside the class can be explicitly made inline by prefixing the keyword inline before the return type of the function in the function header.

How would you define member function outside the class template?

Member functions of class templates (C++ only) You may define a template member function outside of its class template definition. The overloaded addition operator has been defined outside of class X . The statement a + 'z' is equivalent to a. operator+('z') .


2 Answers

Yes, the exact same is true for template classes.

The reason why method definitions for template classes are usually preferred to be inline is that with templates, the entire definition must be visible when the template is instantiated.

So if you put the function definition in some separate .cpp file, you'll get a linker error. The only general solution is to make the function inline, either by defining it inside the class or outside with the inline keyword. but in either cases, it must be visible anywhere the function is called, which means it must typically be in the same header as the class definition.

like image 136
jalf Avatar answered Sep 22 '22 08:09

jalf


There's no difference, aside from having to type more. That includes the template bit, the inline and having to use more "elaborate" names when referring to the class. For example

template <typename T> class A { 
  A method(A a) { 
    // whatever
  } 
}; 

template <typename T> inline A<T> A<T>::method(A a) { 
  // whatever
} 

Note that when the method is defined inside you can always omit the template parameter list <T> when referring to A<T> and just use A. When defining it outside, you have to use the "full" name in the return type and in the name of the method (but not in the parameter list).

like image 28
AnT Avatar answered Sep 23 '22 08:09

AnT