Difference between member functions for a template class defined inside and outside of the class
c++, class, inline, templates
Solution
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.
Problem
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.