gpt4 book ai didi

c++ - 为什么你不能从一个元组中分配一个对,但元组可以从一个对中分配?

转载 作者:可可西里 更新时间:2023-11-01 18:32:27 26 4
gpt4 key购买 nike

我不清楚为什么分配 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;
^

最佳答案

我认为这是另一种情况:

No one proposed it.

首先,您的代码适用于 libc++ (作为扩展)。 libc++ 实现了一个“类似元组”的概念,它包括 tuplepairarray,然后有成员模板(在 tuplepair) 对“类元组”类型进行操作。该方法并非完全没有问题,但它看起来确实很有前途。

关于c++ - 为什么你不能从一个元组中分配一个对,但元组可以从一个对中分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com