两个参数包之间的公共类型 C++
我需要在带有 2 个参数包的求和之后定义返回类型(?????? 在代码中)!我的目标是定义一个 Tuple 类(如 std::tuple)并在这里实现,例如,两个长度相同且类型兼容的元组之间的加法),这是一个需要理解的代码片段:
template<class FirstType, class ... Types>
class Tuple{
....
template <typename ... OtherTypes>
const Tuple<????> operator+(Tuple<OtherTypes...>& other) const{...}
...
}
问题是“int + double" 应该返回一个 double 例如!我查看了 std::common_type 一侧,但我也没有看到。 元组的构造如下:
private:
FirstType m_first ;
Tuple<Types...> m_next ;
m_first 包含值,并且, m_next 通过删除一个值来包含下一个元组。
这是我存储不同类型的方法,尽管还有另一种采用遗传方法的方法。 我有另一个带有递归案例模板的类。
所以我不知道如何获取最终的返回类型,我有一个 get 函数,其中使用了 getHelper<...>结构返回正确的类型,但这里又不同了。我不想使用自动,这会给我带来问题,而且这并不神奇。
预先感谢您的答复。
示例:元组
I need to define the return types (???? in code) after a sum with 2 parameters pack! My objective is to define a Tuple class (like std::tuple) and to implement for example here, the addition between two tuples of the same length and compatible type), here is a code snippet to understand :
template<class FirstType, class ... Types>
class Tuple{
....
template <typename ... OtherTypes>
const Tuple<????> operator+(Tuple<OtherTypes...>& other) const{...}
...
}
The problem is that "int + double" should return me a double for example! I looked on the std::common_type side, but I don't see either.
A tuple is constructed as follows:
private:
FirstType m_first ;
Tuple<Types...> m_next ;
m_first contains the value, and,
m_next contains the next tuple by removing a value.
This is my way of storing the different types although there is another way with a hereditary approach.
I have another class with a template for the recursive case.
So I don't see how to get the final return type, I have a get function where I used a getHelper<...> structure to return the correct type but here it's different again. I don't want to use AUTO which can cause me problems and which is not magic.
Thank you in advance for your answers.
Example : Tuple<int,double>(3,4.4) + Tuple<double,double>(3.1,3.1) = Tuple<DOUBLE,DOUBLE>(6.5,7.5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅此处,了解如何按元素添加两个元组 https://stackoverflow.com/a/50815600/4117728。以下内容基于此答案。剩下要添加的只是获取通用类型元组:
然后可以将上面链接的答案调整为:
但是,
a + b
已经产生通用类型,因此您可以简单地使用自动:
See here for how to elementwise add two tuples https://stackoverflow.com/a/50815600/4117728. The following build on this answer. What is left to add is just to get the common type tuple:
Then the answer linked above can be adjusted to:
However,
a + b
already yields the common type, hence you can simply useauto
: