0

Is it possible to have a template parameter as the template argument in an extern template?

For instance having Bar being extern in the following code

template<typename T>
void Foo() {
    Bar<T>();
}

Extern with specific types such as extern template void Bar<int>(); works. However, I can't get some variant of extern template void Bar<T>(); to work.

Daniel
  • 1
  • 1
  • A template function must be fully available to the compiler. That's why you need to put them in your headers, not your cpp files. Since the template is getting, effectively, copied into each compilation unit, `extern` makes little sense because a template is never external to any unit. – Silvio Mayolo Mar 18 '22 at 16:49
  • [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/) – Remy Lebeau Mar 18 '22 at 17:00
  • To add to what @SilvioMayolo says, declaring a specific extern template like `extern template void Bar();` requires that it also be specifically instantiated somewhere. This is no different than just including the full template def except that it reduces compile time slightly since it can depend on the instantiation existing elsewhere. – doug Mar 18 '22 at 17:01
  • It is in a header, that's not the problem – Daniel Mar 18 '22 at 17:02
  • I have it instantiated somewhere else, but it would be nice not have to write extern for each single type. A bit more context, my specific problem is that Bar contains a cuda kernel so I'm compiling it separately from the rest of the code so that is why it is extern. And I can't compile everything with nvcc since other parts depend on C++20. – Daniel Mar 18 '22 at 17:07

0 Answers0