c++广义运算符模板

发布于 2024-12-06 16:08:33 字数 663 浏览 4 评论 0原文

我正在做一些数值模拟,在向量上重载操作(类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百合的盛世恋 2024-12-13 16:08:33

现在,如果我在某种情况下使用运算符,上述内容确实有效
“A*B”,其中 A 和 B 不同并返回不同的类型。

老实说,这没有道理。您的模板根本不应该工作,因为无法推导 V 并且它是第三个模板参数。如果您写过:

template <typename V, template T, template U>
vector<V> operator*(const vector<T>& A, const vector<U>& B){
   //blah blah
}

这将“起作用”,但仅当您显式指定 V 时,例如

operator*<double>(A, B); //where A is vector<int> and B is vector<float>, for example

Surely you Want to return a vector ,其中 V 是表达式T()*U()。这在 C++11 中是可以做到的,但在 C++03 中则不然(我的意思是,你最多可以做一些类型特征)。下面是 C++11 中的实现方式:

template <typename T, template U>
vector<decltype(T()*U())> operator*(const vector<T>& A, const vector<U>& B)  
{
   //blah blah
}

HTH

Now, the above does indeed work if I use the operator in a situation
"A*B" where A and B are distinct and return a different type.

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:

template <typename V, template T, template U>
vector<V> operator*(const vector<T>& A, const vector<U>& B){
   //blah blah
}

This would "work" but only if you explicitly specified V, something like

operator*<double>(A, B); //where A is vector<int> and B is vector<float>, for example

Surely you want to return a vector<V> where V is the type of the expression T()*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:

template <typename T, template U>
vector<decltype(T()*U())> operator*(const vector<T>& A, const vector<U>& B)  
{
   //blah blah
}

HTH

情定在深秋 2024-12-13 16:08:33

这可以在 C++0x 中使用 decltype 工作。

template <typename T, template U> 
vector<decltype(declval<T>() + declval<U>())> 
operator*(const vector<T>& A, const vector<U>& B){
   //blah blah
}

如果不使用这个机制——并且假设 T 和 U 不提供自己的机制——你就不能做这样的事情。只能处理T、U、返回类型都是同一类型的情况。但是,您可以处理原始类型 - 将 + 等运算符应用于各种原始类型以查找提升的类型的结果有一个 Boost 类型特征。

This can work in C++0x with decltype.

template <typename T, template U> 
vector<decltype(declval<T>() + declval<U>())> 
operator*(const vector<T>& A, const vector<U>& B){
   //blah blah
}

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.

愁杀 2024-12-13 16:08:33

正如其他人指出的,您可以使用 decltype 来实现这一点。 C++0x 还提供了模板 common_type ,它推导出一个类型,所有模板参数都可以强制转换为该类型,而无需任何特定的算术运算。因此,如果参数类型没有可用的重载运算符,也可以使用它。

As others have pointed out you can use decltype to achieve this. C++0x also provides the template common_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文