0

I'm trying to add audio to a game engine I'm working on using OpenAL. OpenAL has a error stack that's only 1 deep, so it has to be polled every request. This function is one of four almost identical ones that pass through the function and return whatever the function would if run normally. This function compiles fine,

struct OpenALFunctions
{
   template<typename alcFunction, typename ReturnType, typename... Params>
   static auto alcCallImpl(
      const char* filename, const std::uint32_t line, alcFunction function, 
      ReturnType& returnValue, ALCdevice* device, Params... params)
      ->typename std::enable_if_t<!std::is_same_v<void, decltype(function(params...))>, bool>
   {
      *** Definition ***
   }
}

but this one doesn't

struct OpenALFunctions
{
   template<typename alcFunction, typename ReturnType, typename... Params>
   static auto alcCallImpl(
      const char* filename, const std::uint32_t line, alcFunction function, 
      ReturnType& returnValue, ALCdevice* device, Params... params)
      ->typename std::enable_if_t<!std::is_same_v<void, decltype(function(params...))>, bool>;
}

template<typename alcFunction, typename ReturnType, typename... Params>
auto OpenALFunctions::alcCallImpl(
   const char* filename, const std::uint32_t line, alcFunction function, 
   ReturnType& returnValue, ALCdevice* device, Params... params)
   ->typename std::enable_if_t<!std::is_same_v<void, decltype(function(params...))>, bool>
{
   *** Definition ***
}

I don't know why the first works and the second doesn't. If you could help me see what I'm screwing up, it would be appreciated.

[ UPDATE ]

When the declaration and definition are both in main.cpp, it compiles

When the declaration is moved to a header and definition is left in main.cpp, it compiles.

When the declaration is moved to a header and the definition is moved to a different .cpp (ex. OpenALFunctions.h & OpenALFunctions.cpp) it gets an unresolved external symbol error (LNK2019). The .cpp starts with #include "OpenALFunctions.h". Still don't know why this doesn't work, but at least now my problem should be more reproduceable.

  • Could be a bug in GCC. Clang does not complain about it. – t.niese Jul 13 '20 at 08:11
  • 2
    Template-based functions are created when used, as of that definition of a template has to be in the header (or visible for the file in which it is used). [Storing C++ template function definitions in a .CPP file](https://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file) – t.niese Jul 13 '20 at 09:58

0 Answers0