c++广义运算符模板
我正在做一些数值模拟,在向量上重载操作(类似于 valarrays)是很好的。例如,我可以写
template <typename T>
vector<T> operator*(const vector<T>& A, const vector<T>& B){
//blah blah
}
但是如果我想概括这个模板以便作用于两种不同类型的向量并(可能)返回第三种类型怎么办?即我想写
template <typename T, template U, template V>
vector<V> operator*(const vector<T>& A, const vector<U>& B){
//blah blah
}
现在,如果我在“A*B”情况下使用运算符,其中 A 和 B 是不同的类型并返回另一个不同的类型,则上述确实有效。但是,如果A和B是同一类型,则不起作用。当然,我可以为每个组合定义不同的模板(即仅 T,或仅 T 和 U,或 T、U 和 V),但这看起来很难看。有没有一种方法可以使用上面给出的 T、U 和 V 品种的单个模板表达式,并使其工作,即使“A”、“B”和“A*B”都是相同类型(或具有只有两种不同类型?)
I'm doing some numerical simulations where it is nice to overload operations on vectors (similar to valarrays). For example, I can write
template <typename T>
vector<T> operator*(const vector<T>& A, const vector<T>& B){
//blah blah
}
But what if I want to generalize this template so as to act on two different types of vectors and (potentially) return a third type? I.e. I want to write
template <typename T, template U, template V>
vector<V> operator*(const vector<T>& A, const vector<U>& B){
//blah blah
}
Now, the above does indeed work if I use the operator in a situation "A*B" where A and B are distinct types and return a another distinct type. However, if A and B are the same type, it does not work. Certainly I could define different templates for each combination (i.e. T only, or T and U only, or T, U, and V) but that seems ugly. Is there a way I can use a single template expression of the T,U, and V variety given above and make it work even if "A", "B", and "A*B" are all the same types (or have only 2 different types?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
老实说,这没有道理。您的模板根本不应该工作,因为无法推导 V 并且它是第三个模板参数。如果您写过:
这将“起作用”,但仅当您显式指定 V 时,例如
Surely you Want to return a
vector
,其中V
是表达式T()*U()
。这在 C++11 中是可以做到的,但在 C++03 中则不然(我的意思是,你最多可以做一些类型特征)。下面是 C++11 中的实现方式:HTH
To be honest, this doesn't make sense. Your template shouldn't work at all because V cannot be deduced and it is the third template parameter. If you had written:
This would "work" but only if you explicitly specified V, something like
Surely you want to return a
vector<V>
whereV
is the type of the expressionT()*U()
. This is possible to do in C++11, but not trivially in C++03( I mean, you could do some type-traiting at best). Here's how it's done in C++11:HTH
这可以在 C++0x 中使用 decltype 工作。
如果不使用这个机制——并且假设 T 和 U 不提供自己的机制——你就不能做这样的事情。只能处理T、U、返回类型都是同一类型的情况。但是,您可以处理原始类型 - 将
+
等运算符应用于各种原始类型以查找提升的类型的结果有一个 Boost 类型特征。This can work in C++0x with decltype.
Without using this mechanism- and presuming that T and U do not provide their own mechanism- you can't do something like this. You can only handle the situation where T, U, and the return type are all the same type. You can, however, deal with primitive types- there's a Boost type trait for the result of applying operators like
+
to various primitive types to find the promoted type.正如其他人指出的,您可以使用 decltype 来实现这一点。 C++0x 还提供了模板
common_type
,它推导出一个类型,所有模板参数都可以强制转换为该类型,而无需任何特定的算术运算。因此,如果参数类型没有可用的重载运算符,也可以使用它。As others have pointed out you can use
decltype
to achieve this. C++0x also provides the templatecommon_type
which deduces a type to which all it template arguments can be coerced without any specific arithmetic operation. So it can also be used if no overloaded operators are available for the argument types.