my problem is similar to - Template function accepting any type similar to map<X, Y> - yet, it didn't solve it, because I'm using already templated object.
typedef struct _foo
{
int x, y, z;
} Foo;
// Note - it cannot be specialized for CBufferInt, CBufferFloat
// because it holds additional informations, left them on purpose
template <class T>
struct CBuffer
{
public:
T value;
UINT8* ptr = nullptr;
ComPtr<ID3D12Resource> resource = NULL;
};
I want to call function like this:
template <typename T>
void TemplateFunction(CBuffer<T> cb);
int main()
{
CBuffer<Foo> cb;
TemplateFunction<Foo>(cb);
}
I've tried multiple configurations:
template <typename T> void TemplateFunction(CBuffer<T> cb);
template <typename T> void TemplateFunction(T cb);
template <template <typename> class CB, typename T> void TemplateFunction(CB<T> cb);
Is it possible to use template the way I desire (i.e. CBuffer) or do I have to create workaround?
Edit #1: Below code created in new class is working. I even went further and replaced CB and Foo with my own structures and it was working correctly.
#pragma once
#ifndef _TEST_STACK_OVERFLOW_H_
#define _TEST_STACK_OVERFLOW_H_
typedef struct _foo
{
public:
XMFLOAT4 a;
XMFLOAT3 b;
XMFLOAT2 c;
} Foo;
template <class T>
struct CB
{
public:
T value;
UINT8* ptr = nullptr;
ComPtr<ID3D12Resource> resource = NULL;
};
template <typename T>
void CreateRaytracingPipeline(CB<T> cb);
class TestStackOverflow
{
public:
TestStackOverflow()
{
CB<Foo> cb{};
CreateRaytracingPipeline(cb);
}
};
#endif // !_TEST_STACK_OVERFLOW_H_
template<typename T>
inline void CreateRaytracingPipeline(CB<T> cb)
{
}
However in my final code, I am having this error message:
Error LNK2019 unresolved external symbol "public: void __cdecl RaytracingResources::CreateRaytracingPipeline<struct _sceneConstantBuffer>(struct CBuffer<struct _sceneConstantBuffer>)" (??$CreateRaytracingPipeline@U_sceneConstantBuffer@@@RaytracingResources@@QEAAXU?$CBuffer@U_sceneConstantBuffer@@@@@Z) referenced in function WinMain RTCP C:\Users\Kamil\Documents\_Projects\RTCP\RTCP\RTCP.obj 1
In this commit, I am providing source code, which is only calling function in question and instantly closes executable instance - https://github.com/komilll/RTCP/tree/3d0b0858420734f7f16c92e7d608b2f19e01d17c Please relate to RTCP.cpp which constists only of code below:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{
HRESULT result = S_OK;
CBuffer<SceneConstantBuffer> cb{};
RaytracingResources* raytracing = new RaytracingResources();
raytracing->CreateRaytracingPipeline(cb);
return 0;
}
Where CBuffer is:
template <class T>
struct CBuffer
{
public:
T value;
UINT8* ptr = nullptr;
ComPtr<ID3D12Resource> resource = NULL;
};
SceneConstantBuffer is:
typedef struct _sceneConstantBuffer
{
XMMATRIX projectionToWorld;
XMFLOAT4 cameraPosition;
XMFLOAT4 lightPosition;
XMFLOAT4 lightAmbientColor;
XMFLOAT4 lightDiffuseColor;
} SceneConstantBuffer;
RaytracingResources is:
class RaytracingResources
{
public:
RaytracingResources() = default;
template <typename T>
void CreateRaytracingPipeline(CBuffer<T> cb);
}
template<typename T>
inline void CreateRaytracingPipeline(CBuffer<T> cb)
{
}