【问题标题】:Why can't you assign a pair from a tuple, but tuple can be assigned from a pair?为什么不能从元组中分配一对,但可以从一对中分配元组?
【发布时间】:2014-02-13 14:02:56
【问题描述】:

我不清楚为什么分配tuple<X,Y>=pair<X,Y>是合法的

但是分配pair<X,Y>=tuple<X,Y>是违法的

    std::pair<int, double> x { 1 , 5.5};
    std::tuple<int, double> y { 1 , 5.5};
    int a;
    double b;
    std::tie(a,b) = x;
    std::tie(a,b) = y;
    x = y;  // THIS LINE (line 12)
    y = x;  // but this is fine ???

这不应该是对称的吗?

使用 g++ 4.8.1 会出现以下错误:

tp.cpp:12:4: error: no match for operator= (operand types are std::pair<int, double> and std::tuple<int, double>)
  x = y;
    ^
tp.cpp:12:4: note: candidates are:
In file included from /opt/gcc-4.8.1/include/c++/4.8.1/utility:70:0,
                 from /opt/gcc-4.8.1/include/c++/4.8.1/tuple:38,
                 from tp.cpp:1:
/opt/gcc-4.8.1/include/c++/4.8.1/bits/stl_pair.h:158:7: note: std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_T1, _T2>&) [with _T1 = int; _T2 = double]
       operator=(const pair& __p)
       ^
/opt/gcc-4.8.1/include/c++/4.8.1/bits/stl_pair.h:158:7: note:   no known conversion for argument 1 from std::tuple<int, double> to const std::pair<int, double>&
/opt/gcc-4.8.1/include/c++/4.8.1/bits/stl_pair.h:166:7: note: std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_T1, _T2>&&) [with _T1 = int; _T2 = double]
       operator=(pair&& __p)
       ^
/opt/gcc-4.8.1/include/c++/4.8.1/bits/stl_pair.h:166:7: note:   no known conversion for argument 1 from std::tuple<int, double> to std::pair<int, double>&&
/opt/gcc-4.8.1/include/c++/4.8.1/bits/stl_pair.h:177:2: note: template<class _U1, class _U2> std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(const std::pair<_U1, _U2>&) [with _U1 = _U1; _U2 = _U2; _T1 = int; _T2 = double]
  operator=(const pair<_U1, _U2>& __p)
  ^
/opt/gcc-4.8.1/include/c++/4.8.1/bits/stl_pair.h:177:2: note:   template argument deduction/substitution failed:
tp.cpp:12:4: note:   std::tuple<int, double> is not derived from const std::pair<_T1, _T2>
  x = y;
    ^
In file included from /opt/gcc-4.8.1/include/c++/4.8.1/utility:70:0,
                 from /opt/gcc-4.8.1/include/c++/4.8.1/tuple:38,
                 from tp.cpp:1:
/opt/gcc-4.8.1/include/c++/4.8.1/bits/stl_pair.h:186:2: note: template<class _U1, class _U2> std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U1 = _U1; _U2 = _U2; _T1 = int; _T2 = double]
  operator=(pair<_U1, _U2>&& __p)
  ^
/opt/gcc-4.8.1/include/c++/4.8.1/bits/stl_pair.h:186:2: note:   template argument deduction/substitution failed:
tp.cpp:12:4: note:   std::tuple<int, double> is not derived from std::pair<_T1, _T2>
  x = y;
    ^

【问题讨论】:

  • 因为元组是通用的,但是一对只能包含两个元素。当然,tuple 知道如何处理pair:如果它的赋值运算符遇到一对,并注意到元组有两个元素,那么它就会进行赋值。然而,一对并不知道从通用 n 元组到一对的转换,因为 tuple 可能有 any 个元素。
  • @H2CO3 但是为什么不使用恰好 2 个元素的 tuple
  • @MarkGarcia 我认为可以做到,但显然,决定不需要在标准库中包含这样的自动转换。
  • pair 首先出现在 C++98 中。更通用的 tuple 在 C++11 中排在最后。假设pair 不支持从两个元素tuple 分配是正确的,那么简单地说,pair 没有使用该功能进行改造。
  • 我想有些人会很乐意弃用 std::pair ,因为有了 std::tuple...

标签: c++ c++11 std-pair stdtuple


【解决方案1】:

我认为这是另一种情况:

没有人提议。

Fwiw,您的代码适用于 libc++(作为扩展名)。 libc++ 实现了一个“类似元组”的概念,包括tuplepairarray,然后有成员模板(在tuplepair)上运行“类似元组”类型。这种方法并非完全没有问题,但看起来确实很有希望。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 2017-02-01
    • 2011-10-11
    • 2018-01-24
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    相关资源
    最近更新 更多