ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c-为什么不能从元组分配对,但是可以从对分配元组?

2019-10-10 18:06:41  阅读:247  来源: 互联网

标签:std-pair c c11 stdtuple


我不清楚为什么分配元组< X,Y> = pair< X,Y>

但是分配对X,Y =元组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;
    ^

解决方法:

我认为这是另一种情况:

No one proposed it.

首先,您的代码适用于libc++(作为扩展名). libc实现了一个“类似元组”的概念,该概念包括元组,对和数组,然后具有对“类元组”类型进行操作的成员模板(在元组和对上).这种方法并非完全没有问题,但确实有希望.

标签:std-pair,c,c11,stdtuple
来源: https://codeday.me/bug/20191010/1888028.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有