0

I have this function defined in header:

template<typename T>
void GfxDevice::BindConstantBuffer(unsigned int stage, GfxConstantBuffer<T>* constantBuffer, unsigned int binding)
{
    GfxBufferResource* bufferResource = constantBuffer->GetBufferResource();
    if (!bufferResource->Initialized())
        bufferResource->Initialize();

    BindConstantBuffer(stage, bufferResource->GetBuffer(), binding);
}

I get linker error because compiler won't link Initialize() function that is defined in .cpp file. Is there any way of resolving that and that Initialize() stay in the cpp?

  • https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – πάντα ῥεῖ Sep 26 '21 at 16:13
  • 1
    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) – Richard Critten Sep 26 '21 at 16:13
  • Yes, I am aware of the reason, but I am wondering how to do this, because GfxBufferResource doesn't depend on T, so there must be a way, but having hard time keeping pretty and optimized code and not having linker errors, so wanted to see how are this problems resolved :) – Angry Tomato Sep 26 '21 at 16:16
  • Templates are really pain in the ass – Angry Tomato Sep 26 '21 at 16:16
  • This function is in ".hpp" file ( that is suggested by the link) , not exactly in header, but in the end it is the same thing, it will slow down compile time if I add bunch of libs there that are used in Initialize() function – Angry Tomato Sep 26 '21 at 16:17

1 Answers1

0

Ok, I was just being stupid by frustration that template programming gave me :)

The solution is not that complicated i just needed to pass GfxBufferResource* in the BindConstantBuffer instead of doing that stuff in template function.

So solution:

    template<typename T>
void GfxDevice::BindConstantBuffer(unsigned int stage, GfxConstantBuffer<T>* constantBuffer, unsigned int binding)
{
    BindConstantBuffer(stage, constantBuffer->GetBufferResource(), binding);
}

and make BindConstantBuffer in GfxDevice that will receive GfxBufferResource and do it there.