0

I have the following signature of a template:

// LoggingImpl.hpp
template <typename... Ts>
static void LogFatal(int32_t lineNumber, const char* function, const char* format, const Ts&... args) noexcept;

The definition of the function resides in the corresponding .cpp file:

// LoggingImpl.cpp
template <typename... Ts>
static void LoggingImpl::LogFatal(int32_t lineNumber, const char* function, const char* format, const Ts&... args) noexcept
{
    // some function definition here.
}

The .cpp file also contains the explicit definition so that I do not get the linker error.

// LoggingImpl.cpp
template void LoggingImpl::LogFatal<char*>(int32_t, const char*, const char*, const char*) noexcept;

I am on a Win 10 machine, using visual studio 2019 compiling using MSVC.

Question: How can I have definition of the template function separated into the source (.cpp) file when my template contains variadic functions. I don't know how to add explicit signature when the template is for variadic arguments.

jdaz
  • 5,964
  • 2
  • 22
  • 34
  • Templates must be in the header. They are only defined when they are seen for the first time in use with a specific type. – TigerChan Aug 04 '20 at 07:25
  • 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) – Const Aug 04 '20 at 07:26
  • Thanks @Const. But I am having issue in separating template functions that have variadic arguments which is not handled in the link. I am trying to use the "explicit instantiation in .cpp file" trick but I don't know how to do that when arguments are variadic. – vandit jain Aug 04 '20 at 16:19

0 Answers0