Using nested structure as template

Started by
4 comments, last by Matt-D 10 years, 8 months ago

Hello there,

I'm trying to build a class that is responsible for loading and managing a vertex shader.

The idea is to have a separate class for managing the constant buffer (the data a vertex shader needs.)

This class looks like this:

dxman_cbuffer.h


#pragma once
#include "dxman_common.h"
#include <D3D11.h>

template <typename Buffer>
class ConstantBuffer
{
private:
	ID3D11Buffer *_buffer;
	const Buffer *_content;
public:
	HRESULT Init(ID3D11Device* dev, Buffer* initialData);
	void Exit();
	ID3D11Buffer* GetBuffer() const;
	HRESULT SetBufferData (ID3D11DeviceContext* devcon, Buffer *content);
	const Buffer* GetBufferData() const;
};

The problem occours in this class:

ColorShader.h


#pragma once
#include "common.h"
#include "dxman_shader.h"
#include "dxman_cbuffer.h"

class VSColor : public IVertexShader
{
private:
	struct SMatrixBuffer
	{
		XMMATRIX worldViewProj;
	};
	ConstantBuffer<SMatrixBuffer> _cbPerObject;
public:
	virtual HRESULT Init (ID3D11Device *dev); //Loads the shader & creates the constant buffer
	virtual void Exit(); //Releases all resources 
	virtual void Activate (ID3D11DeviceContext* devcon); //Sets the shader and the constant buffer to the Vertex Shader stage
	virtual HRESULT UpdateMatrixBuffer (ID3D11DeviceContext *devcon, XMMATRIX &worldViewProj); //Updates the buffer data
};

Since the template class ConstantBuffer<SMatrixBuffer> is never declared, the linker can't find any symbols. The problem is that I can't just write ConstantBuffer<SMatrixBuffer> in dxman_cbuffer.cpp (where the ConstantBuffer class is) since SMatrixBuffer is declared as private in another class. (And I want to keep both files separated!)

Advertisement

Shouldn't it be "ConstantBuffer<struct SMatrixBuffer> _cbPerObject;" ?

Long story short, you can't put template function definitions in a source file without special compiler support that only a very few compilers have ever had. You need to move the template function definitions from dxman_cbuffer.cpp to the header.

Just an addition to what SiCrane said, the error you're having about missing symbols is because the compiler cannot find the function definitions when places in the .cpp. So move all your code in the .cpp in the header file. But if all you're after is just a physical file separation, you could create a separate file call it something like dxman_cbuffer.hpp. Put all your function definitions there then include that .hpp file at the bottom of the header file of your dxman_cbuffer.h.

OK, it works. Altough I really have no clue why... happy.png

Since you're only using "Buffer" type indirectly, i.e., as a "Buffer *" (ptr-to-Buffer), I suppose you could try a workaround (if you're willing to change your ConstantBuffer class), with forward declaration of "SMatrixBuffer" and using "SMatrixBuffer *" as a template argument in the instantiation. Beforehand, you'd need an explicit template instantiation in the same translation unit (extra or existing file, whatever you find most convenient) that has access to the forward-declared template argument.

For more details / examples, see:

http://www.network-theory.co.uk/docs/gccintro/gccintro_60.html

http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc16explicit_instantiation.htm

http://en.cppreference.com/w/cpp/language/class_template#Explicit_instantiation

Since you're only ever going to use incomplete types, this will even work with smart pointers, like std::unique_ptr (using which you should strongly consider, since it's zero-overhead anyway -- otherwise, by leaving raw pointers everywhere you're keeping your ownership semantics assumptions unstated, undocumented, and unenforced, which is rather bad):

http://stackoverflow.com/questions/10065384/instantiation-of-a-list-with-an-incomplete-type-in-a-typedef

If it sounds involved, it's because it is ;-) If you're fine with moving to a header-only template implementation, it's probably the simpler / recommended way.

In any case, reconsider using raw pointers, http://klmr.me/slides/modern-cpp/ ;-)

This topic is closed to new replies.

Advertisement