4

I've been wondering about this for a while, stackoverflow had a bunch of related, but not quite the same questions, so I'm asking it here.

Is it possible for a templated class to have methods in the cpp that do not depend on this template? Evidently these methods aren't affected by the template, so the compiler should be able to compile them separately.

If not possible, what would be a workaround, if I really, really want to separate this code?

template<typename T>
class MyAwesomeVectorClone{
  size_t size;
  size_t capacity;
  T* data;

  bool doesSizeExceedCapacity(); // non template method, define in cpp?
  void add(T& t){// template method
  }
}
bool MyAwesomeVectorClone::doesSizeExceedCapacity() {
  return size > capacity;
}
5
  • 1
    Put the non templated parts in a (non-templated) base class and privately inherit from it? Commented Apr 8, 2019 at 18:13
  • 1
    Not sure what you mean by compiled separately, but yes you can place them in the source file although you still need the template parameter in the function signature: template <class T> bool MyAwesomeVectorClone<T>::doesSizeExceedCapacity() { return size > capacity; } Commented Apr 8, 2019 at 18:14
  • @Borgleader That solution would work, if there's nothing cleaner I'll be going with that Commented Apr 8, 2019 at 18:16
  • @Mansoor Would the compiler notice that it doesn't use T, and then allow it? I'll try it Commented Apr 8, 2019 at 18:17
  • @lennartVH01 whether or not the parameter is used is unimportant, the template parameter is necessary to define the member function correctly. Commented Apr 9, 2019 at 15:16

2 Answers 2

4

Is it possible for a templated class to have methods in the cpp that do not depend on this template?

No. A class template is not a specific type, you create a type to which these memeber functions can belong only when instantiating the template. So it's impossible to treat member functions that don't depend on the template type parameter differently from the rest of the class template.

What you can do, however, is extracting the parameter-independent parts out into a separate class or separate free functions. This is the subject of Item 44 in Effective C++ by Scott Meyers ("Factor parameter-independent code out of templates"). He presents a matrix example, where the parameter-independent code is moved into a base class from which the actual class template privately inherits. But composition is fine, too, of course.

Sign up to request clarification or add additional context in comments.

Comments

0

Another solution is to add the function, and any other helper functions, in a namespace that clearly indicates that it contains functions useful for implementing the class template.

namespace MyAwesomeVectorClone_Impl_Details
{
   bool doesSizeExceedCapacity();
}

Those functions can be defined in a .cpp file.

2 Comments

But you can't really access the object's private state doing that
@lennartVH01, true. In that case, you need to pass the necessary parameters to the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.