[英]How can I wrap a template function with another template function (specifically glm::dot)?
I'm using the glm math library, which offers many vector/matrix structs and operations (for use in graphics).我正在使用 glm 数学库,它提供了许多向量/矩阵结构和操作(用于图形)。 It has types for many different vector sizes (
vec2
, vec3
, vec4
, etc...) and also a really clean API to do general operations on them, regardless of size ( dot(a,b)
, length(a)
, etc...).它具有许多不同矢量大小的类型(
vec2
、 vec3
、 vec4
等),还有一个非常干净的 API 可以对它们进行一般操作,无论大小( dot(a,b)
、 length(a)
等...)。
One function I would like is to determine the "length squared" of a vector (as it's computationally much cheaper to compare lengths this way), which is essentially this function: float lengthSquared(v) { return glm::dot(v,v); }
我想要的一个 function 是确定向量的“长度平方”(因为以这种方式比较长度在计算上要便宜得多),本质上就是这个 function:
float lengthSquared(v) { return glm::dot(v,v); }
float lengthSquared(v) { return glm::dot(v,v); }
I'd like to create such a function: how might I go about doing it?我想创建这样一个function:我怎么可能会做go呢?
The template signature for dot
is as follows: dot
的模板签名如下:
template <class T, glm::qualifier Q>
T dot(const glm::qua<T,Q> &x, const glm::qua<T,Q> &y)
So my best attempt to wrap is is as follows:所以我最好的包装尝试如下:
template <class T, glm::qualifier Q>
T lengthSquared(const glm::qua<T,Q> &x)
{ return glm::dot(x,x); }
But I get many errors including 'lengthSquared': no matching overloaded function found
(when calling it with a vec2
), and [signature here]: could not deduce template argument for 'const glm::qua<T,Q> &' from 'glm::vec2'
.但我得到了很多错误,包括
'lengthSquared': no matching overloaded function found
(当用vec2
调用它时),和[signature here]: could not deduce template argument for 'const glm::qua<T,Q> &' from 'glm::vec2'
。
I don't really understand how template "qualifiers" work, so I'm flying a bit blind here.我真的不明白模板“限定符”是如何工作的,所以我在这里有点盲目。
Thanks!谢谢!
edit: here's the declaration of vec2
: typedef glm::vec<2,float,glm::packed_highp> glm::vec2;
编辑:这里是
vec2
的声明: typedef glm::vec<2,float,glm::packed_highp> glm::vec2;
and here's a sample call to lengthSquared
: vec2 x = {0.5,0.5}; float ls = lengthSquared(x);
这是对
lengthSquared
的示例调用: vec2 x = {0.5,0.5}; float ls = lengthSquared(x);
vec2 x = {0.5,0.5}; float ls = lengthSquared(x);
Your function definition is:您的 function 定义是:
template <class T, glm::qualifier Q>
T lengthSquared(const glm::qua<T,Q> &x)
{ return glm::dot(x,x); }
but you are calling this function with an argument of type glm::vec<2,float,glm::packed_highp>
.但是您使用
glm::vec<2,float,glm::packed_highp>
类型的参数调用此 function 。
Instead, you can write the function like this:相反,您可以像这样编写 function:
template <class T>
auto lengthSquared(const T &x)
{ return glm::dot(x,x); }
and let the compiler deduce both the argument type, and the return type.并让编译器推断出参数类型和返回类型。
Even though it seemingly doesn't work for you I post a simple C++20 alternative that is supported by current versions of GCC and Clang:尽管它似乎对您不起作用,但我发布了一个简单的 C++20 替代方案,当前版本的 GCC 和 Clang 支持该替代方案:
auto lengthSquared(const auto &v) { return glm::dot(v,v); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.