Question

I'm not clear why it is legal to assign tuple<X,Y>=pair<X,Y>

But it is illegal to assign 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 ???

Shouldn't this be symmetrical?

Using g++ 4.8.1 gives the following errors:

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;
    ^
Was it helpful?

Solution

I think this is another case of:

No one proposed it.

Fwiw, your code works with libc++ (as an extension). libc++ implemented a "tuple-like" concept which includes tuple, pair and array, and then has member templates (on tuple and pair) that operate on "tuple-like" types. The approach hasn't been completely without problems, but it does seem promising.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top