两个参数包之间的公共类型 C++

发布于 2025-01-11 07:12:22 字数 900 浏览 3 评论 0原文

我需要在带有 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<...>结构返回正确的类型,但这里又不同了。我不想使用自动,这会给我带来问题,而且这并不神奇。

预先感谢您的答复。

示例:元组(3,4.4) + 元组(3.1,3.1) = 元组(6.5,7.5)

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 技术交流群。

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

发布评论

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

评论(1

深白境迁sunset 2025-01-18 07:12:22

请参阅此处,了解如何按元素添加两个元组 https://stackoverflow.com/a/50815600/4117728。以下内容基于此答案。剩下要添加的只是获取通用类型元组:

template <typename T1,typename T2>
struct common_tuple;

template <typename...T1,typename...T2>
struct common_tuple< std::tuple<T1...>,std::tuple<T2...>> {
    using type = std::tuple<std::common_type_t<T1,T2>...>;
};

然后可以将上面链接的答案调整为:

namespace internal
{    
    template<typename T,typename S, size_t... Is>
    common_tuple<T,S>::type add_rhs_to_lhs(T& t1, const S& t2, std::integer_sequence<size_t, Is...>)
    {
        return std::make_tuple( (std::get<Is>(t1) + std::get<Is>(t2))... );      
    }
}

template <typename...T,typename...S>
std::tuple<std::common_type_t<T,S>...> operator + (std::tuple<T...> lhs, const std::tuple<S...>& rhs)
{
   return internal::add_rhs_to_lhs(lhs, rhs, std::index_sequence_for<T...>{});
}


int main(int argc, char *argv[])
{
    auto a = std::make_tuple(1,2,4);
    auto b = std::make_tuple(1.0,2.0,4.0);
    auto c = a + b;
    std::cout << std::get<0>(c) << "\n";
    std::cout << std::get<1>(c) << "\n";
    std::cout << std::get<2>(c) << "\n";
}

但是, a + b 已经产生通用类型,因此您可以简单地使用 自动:

#include <tuple>
#include <iostream>


namespace internal
{    
    template<typename T,typename S, size_t... Is>
    auto add_rhs_to_lhs(T& t1, const S& t2, std::integer_sequence<size_t, Is...>)
    {
        return std::make_tuple( (std::get<Is>(t1) + std::get<Is>(t2))... );      
    }
}

template <typename...T,typename...S>
auto operator + (std::tuple<T...> lhs, const std::tuple<S...>& rhs)
{
   return internal::add_rhs_to_lhs(lhs, rhs, std::index_sequence_for<T...>{});
}


int main(int argc, char *argv[])
{
    auto a = std::make_tuple(1,2,4);
    auto b = std::make_tuple(1.0,2.0,4.0);
    auto c = a + b;
    std::cout << std::get<0>(c) << "\n";
    std::cout << std::get<1>(c) << "\n";
    std::cout << std::get<2>(c) << "\n";
}

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:

template <typename T1,typename T2>
struct common_tuple;

template <typename...T1,typename...T2>
struct common_tuple< std::tuple<T1...>,std::tuple<T2...>> {
    using type = std::tuple<std::common_type_t<T1,T2>...>;
};

Then the answer linked above can be adjusted to:

namespace internal
{    
    template<typename T,typename S, size_t... Is>
    common_tuple<T,S>::type add_rhs_to_lhs(T& t1, const S& t2, std::integer_sequence<size_t, Is...>)
    {
        return std::make_tuple( (std::get<Is>(t1) + std::get<Is>(t2))... );      
    }
}

template <typename...T,typename...S>
std::tuple<std::common_type_t<T,S>...> operator + (std::tuple<T...> lhs, const std::tuple<S...>& rhs)
{
   return internal::add_rhs_to_lhs(lhs, rhs, std::index_sequence_for<T...>{});
}


int main(int argc, char *argv[])
{
    auto a = std::make_tuple(1,2,4);
    auto b = std::make_tuple(1.0,2.0,4.0);
    auto c = a + b;
    std::cout << std::get<0>(c) << "\n";
    std::cout << std::get<1>(c) << "\n";
    std::cout << std::get<2>(c) << "\n";
}

However, a + b already yields the common type, hence you can simply use auto:

#include <tuple>
#include <iostream>


namespace internal
{    
    template<typename T,typename S, size_t... Is>
    auto add_rhs_to_lhs(T& t1, const S& t2, std::integer_sequence<size_t, Is...>)
    {
        return std::make_tuple( (std::get<Is>(t1) + std::get<Is>(t2))... );      
    }
}

template <typename...T,typename...S>
auto operator + (std::tuple<T...> lhs, const std::tuple<S...>& rhs)
{
   return internal::add_rhs_to_lhs(lhs, rhs, std::index_sequence_for<T...>{});
}


int main(int argc, char *argv[])
{
    auto a = std::make_tuple(1,2,4);
    auto b = std::make_tuple(1.0,2.0,4.0);
    auto c = a + b;
    std::cout << std::get<0>(c) << "\n";
    std::cout << std::get<1>(c) << "\n";
    std::cout << std::get<2>(c) << "\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文