[英]Using a member function pointer with a template argument
I am currently trying to make a universal function for setting various values inside of another object's class (specifically the Shader
class).我目前正在尝试创建一个通用函数,用于在另一个对象的类(特别是
Shader
类)中设置各种值。 Here are some examples of what I am trying to set:以下是我尝试设置的一些示例:
void setBool(const std::string& name, bool value) const {
glUniform1i(glGetUniformLocation(shaderProgram, name.c_str()), (int)value);
}
void setInt(const std::string& name, int value) const {
glUniform1i(glGetUniformLocation(shaderProgram, name.c_str()), value);
}
void setFloat(const std::string& name, float value) const {
glUniform1f(glGetUniformLocation(shaderProgram, name.c_str()), value);
}
void setVec2(const std::string& name, const glm::vec2& value) const {
glUniform2fv(glGetUniformLocation(shaderProgram, name.c_str()), 1, &value[0]);
}
void setVec3(const std::string& name, const glm::vec3& value) const {
glUniform3fv(glGetUniformLocation(shaderProgram, name.c_str()), 1, &value[0]);
}
void setVec4(const std::string& name, const glm::vec4& value) const {
glUniform4fv(glGetUniformLocation(shaderProgram, name.c_str()), 1, &value[0]);
}
void setMat2(const std::string& name, const glm::mat2& mat) const {
glUniformMatrix2fv(glGetUniformLocation(shaderProgram, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
void setMat3(const std::string& name, const glm::mat3& mat) const {
glUniformMatrix3fv(glGetUniformLocation(shaderProgram, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
void setMat4(const std::string& name, const glm::mat4& mat) const {
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
Here is the function that I am trying to use:这是我尝试使用的功能:
template <class T>
void setUniforms(void(Shader::*fp)(const std::string&, const T&), const std::string& name, T value) {
for (auto iter = shaderMap.cbegin(); iter != shaderMap.cend(); ++iter) {
(iter->second->*fp)(name, T(value));
}
};
Here is my function call:这是我的函数调用:
manager.setUniforms<const glm::mat4&>(&Shader::setMat4, "view", Camera::getLookMat());
Where Camera::getLookMat()
returns a glm::mat4
.其中
Camera::getLookMat()
返回glm::mat4
。 However, when I try to compile, I get this error:但是,当我尝试编译时,出现此错误:
Error C2664 'void ShaderManager::setUniforms<const glm::mat4&>(void (__thiscall Shader::* )(const std::string &,T),const std::string &,T)': cannot convert argument 1 from 'void (__thiscall Shader::* )(const std::string &,const glm::mat4 &) const' to 'void (__thiscall Shader::* )(const std::string &,T)' C2MEngine D:\Programming\C++\C2MEngine\src\testing\Main.cpp 85
Is there any way that I could reconfigure the function to work with the listed functions?有什么方法可以重新配置函数以使用列出的函数? All functions in this post are member functions, with the second code block being part of a separate class than the first block.
这篇文章中的所有函数都是成员函数,第二个代码块是与第一个代码块不同的类的一部分。 I'm unsure if I'm having problems with either the member function pointer or the template.
我不确定成员函数指针或模板是否有问题。
You are missing a const
qualifier in your member-function pointer:您的成员函数指针中缺少
const
限定符:
template <class T>
void setUniforms(void(Shader::*fp)(const std::string&, const T&) const, const std::string& name, T value)
// added const here ~~~~^
While all of your setters are (surprisingly!) const
-qualified.虽然你所有的 setter 都是(令人惊讶的!)
const
限定的。
Reformatting that error message:重新格式化该错误消息:
Error C2664
'void ShaderManager::setUniforms<const glm::mat4&>(
void (__thiscall Shader::* )(const std::string &,T),
const std::string &,T)':
cannot convert argument 1 from
'void (__thiscall Shader::* )(const std::string &,const glm::mat4 &) const'
to
'void (__thiscall Shader::* )(const std::string &,T)'
C2MEngine D:\Programming\C++\C2MEngine\src\testing\Main.cpp 85
One difference in the types is of course the general template parameter T
in place of a specific type const glm::mat4&
.类型的一个区别当然是通用模板参数
T
代替了特定类型const glm::mat4&
。 You specified the template argument as const glm::mat4&
, so that should be fine.您将模板参数指定为
const glm::mat4&
,所以应该没问题。 The other difference is what's causing an issue: note the const
at the end of the first function pointer type.另一个区别是导致问题的原因:注意第一个函数指针类型末尾的
const
。
A const
on a member function type is part of the function type, and there are no standard conversions for pointers to those types, to make sure const-correctness is not accidentally bypassed.成员函数类型上的
const
是函数类型的一部分,指向这些类型的指针没有标准转换,以确保不会意外绕过 const 正确性。
So add the final const
to the function pointer type:所以将最后的
const
添加到函数指针类型中:
template <class T>
void setUniforms(void(Shader::*fp)(const std::string&, const T&) const,
const std::string& name,
T value);
(Modified from the declaration in your question, although the error message implies the function pointer's value parameter has type T
, not const T&
.) (从您问题中的声明中修改,尽管错误消息暗示函数指针的值参数具有类型
T
,而不是const T&
。)
If you wanted, you could also use an alias template to make that less verbose, and the name of the fp
function parameter more obvious:如果需要,您还可以使用别名模板来减少冗长,并且
fp
函数参数的名称更明显:
template <class T> using ShaderSetFuncPtr =
void (Shader::*)(const std::string&, const T&); // or ,T ?
template <class T>
void setUniforms(ShaderSetFuncPtr<T> fp, const std::string& name, T value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.