0

嗨,我想实现元组。让我知道这出了什么问题以及如何正确实施它。我想从一个函数返回三个值,其中第一个值是整数,最后一个值是数组。

template <typename T1, typename T2, typename T3>
struct t_untuple
{
    T1& a1;
    T2& a2;
    T3& a3;
    explicit t_untuple(T1& a1, T2& a2, T3& a3) : a1(a1), a2(a2), a3(a3) { }

t_untuple<T1, T2, T3>& operator = (const tuple <T1, T2, T3>& p)
    {
        a1 = p.first;
        a2 = p.second;
        a3 = p.third;
        return *this;
    }
};

// Our functor helper (creates it)
template <typename T1, typename T2, typename T3>
t_untuple<T1, T2, T3> unpair(T1& a1, T2& a2, T3& a3)
{
    return t_unpair<T1, T2, T3>(a1, a2, a3);
}

帮我解决这个问题。

我得到Symbol 'tuple' could not be resolved at const tuple & p 因为 p.third 也是一个错误

4

3 回答 3

0

看起来您尝试使用unpairand扩展代码std::pair

t_untuple<T1, T2, T3>& operator = (const tuple <T1, T2, T3>& p)
    {
        a1 = p.first;
        a2 = p.second;
        a3 = p.third;
        return *this;
    }
};

但是元组的访问字段以不同的方式完成:

t_untuple<T1, T2, T3>& operator = (const std::tuple <T1, T2, T3>& p)
    {
        a1 = std::get<0>(p);
        a2 = std::get<1>(p);
        a3 = std::get<2>(p);
        return *this;
    }
};

您还需要在复制粘贴 中替换unpair为:untuple

template <typename T1, typename T2, typename T3>
t_untuple<T1, T2, T3> untuple(T1& a1, T2& a2, T3& a3)
{
    return t_untuple<T1, T2, T3>(a1, a2, a3);
}

不要忘记#include <tuple>,您必须确保您的编译器至少处于 c++11 模式

于 2015-11-20T18:31:19.767 回答
0

假设数组类型是不变的。要么使用 std::tuple

std::tuple<int,int,std::array<std::string>> MyFunc()

这是手册页http://en.cppreference.com/w/cpp/utility/tuple

或创建具体类

struct IIA
{
    int a;
    int b;
    std::array<std::string> arr;
}

IAA Myfunc(){}
于 2015-11-20T18:32:44.243 回答
0

如果您知道要返回什么类型,为什么不使用简单的结构:

template <typename T>
struct return_type {
    int a;
    int b;
    std::array<T> c;
}
于 2015-11-20T18:31:13.367 回答