0

Is there a way to use a template with a function prototype specified in a class definition, the definition of which is found in another cpp file? Example

TEST.h

class TEST {
public:
    TEST()=default;
    template <class test_name>
    test_name func();
private:
 ...
}

TEST.cpp

template <class test_name>
test_name func() {
      ...
}

As far as I know, the template cannot be used with function prototypes, it is necessary to define the function. But why? What are the benefits of this approach?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
ccaniel
  • 27
  • 5
  • 5
    Does this answer your question? [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Guillaume Racicot Nov 05 '20 at 20:33
  • 1
    You'd need to explicitly instantiate it I guess. Once `TEST.cpp` is compiled it won't be possible to instantiate `func` for any other classes anymore. Why do you want the function template `func` to be in the `.cpp` file? – Ted Lyngmo Nov 05 '20 at 20:33

0 Answers0