这可能是我编写的最复杂的模板代码。
我有以下使用已声明为模板的类型:
template <class Vertex, class Pixel>
using VS_TYPE = Pixel(*)(Vertex & v);
template <class Vertex, class Pixel, class Mesh>
using VS_THIS_TYPE = Pixel(Mesh::*)(Vertex & v);
template <class Pixel>
using PS_TYPE = Vec4(*)(Pixel & sd, const Sampler<Pixel> & sampler);
template <class Pixel, class Mesh>
using PS_THIS_TYPE = Vec4(Mesh::*)(Pixel & sd, const Sampler<Pixel> & sampler);
它们是带有模板化参数和返回值的函数指针类型。具有“ THIS”的是指向成员函数(Mesh类的成员)的指针。 我声明了此模板化函数,该函数接受以下类型的一些参数:
template <class Vertex, class Pixel, class Mesh>
void DrawElementArray(Mesh* mesh, Vertex* vertices, VS_THIS_TYPE<Vertex, Pixel, Mesh> VertexShader, PS_THIS_TYPE<Pixel, Mesh> PixelShader)
{
// body of this function
boundObject = (void*)mesh;
_DrawElementArray<Vertex, Pixel, Mesh, VS_THIS_TYPE<Vertex, Pixel, Mesh>, PS_THIS_TYPE<Pixel, Mesh>>(..., VertexShader, PixelShader);
}
VertexShader和PixelShader参数属于这些模板化函数指针类型。这是_DrawElementArray函数的声明:
template <class Vertex, class Pixel, class Mesh, typename VSPtr, typename PSPtr>
void _DrawElementArray(Vertex* vertices, VSPtr VertexShader, PSPtr PixelShader)
{
// bound object is the Mesh* parameter passed into DrawElementArray
// There is another DrawElementArray not shown that does not have this parameter, used for passing in Vertex
// Shader and PixelShader function pointers that are not member functions
//
if (boundObject == nullptr) {
// call vertex shader (parameter unrelated)
VertexShader(vertices[i3])
} else {
// call the member function on the object
Mesh* mesh = (Mesh*)boundObject;
(mesh->*VertexShader)(vertices[i1])
}
}
当我尝试调用VertexShader(函数指针参数)时,问题就来了。它给出了以下错误:
term does not evaluate to a function taking 1 arguments
->* : illegal right operand has type VSPtr
我能够调用DrawElementArray而不看到任何编译错误。有人知道如何解决这些错误吗?基本上我想做的是编写一个函数(_DrawElementArray),该函数可以将函数指针指向成员函数或非成员函数