simulate
simulate

Reputation: 1263

Converting tuple using user-defined conversions

I think this code explains best what I am trying to do:

#include <tuple>

class B
{
    public:
    B()
    { }
};

class A
{
    public:
    A() 
    { }

    operator B()
    {
        return B();
    }
};

int main()
{
    std::tuple<A, A> tup_a;
    std::tuple<B, B> tup_b;

    tup_b = tup_a;
    return 0;
}

So I would like to convert all elements of a tuple to a different type by a user-defined conversion, but the code fails because of some qualifier mismatches inside of tuple during the conversion. How do I make this conversion work?

Here is the error message:

In file included from tst.cpp:1:0:
/usr/include/c++/7/tuple: In instantiation of ‘std::_Tuple_impl<_Idx, _Head, _Tail ...>& std::_Tuple_impl<_Idx, _Head, _Tail ...>::operator=(const std::_Tuple_impl<_Idx, _UElements ...>&) [with _UElements = {A, A}; long unsigned int _Idx = 0; _Head = B; _Tail = {B}]’:
/usr/include/c++/7/tuple:1227:36:   required from ‘std::tuple<_T1, _T2>& std::tuple<_T1, _T2>::operator=(const std::tuple<_U1, _U2>&) [with _U1 = A; _U2 = A; _T1 = B; _T2 = B]’
tst.cpp:27:13:   required from here
/usr/include/c++/7/tuple:313:19: error: ambiguous overload for ‘operator=’ (operand types are ‘B’ and ‘const A’)
    _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
    ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tst.cpp:3:7: note: candidate: constexpr B& B::operator=(const B&) <near match>
 class B
       ^
tst.cpp:3:7: note:   conversion of argument 1 would be ill-formed:
In file included from tst.cpp:1:0:
/usr/include/c++/7/tuple:313:19: error: invalid user-defined conversion from ‘const A’ to ‘const B&’ [-fpermissive]
    _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
    ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tst.cpp:16:5: note: candidate is: A::operator B() <near match>
     operator B()
     ^~~~~~~~
tst.cpp:16:5: note:   passing ‘const A*’ as ‘this’ argument discards qualifiers
In file included from tst.cpp:1:0:
/usr/include/c++/7/tuple:313:19: error: passing ‘const A’ as ‘this’ argument discards qualifiers [-fpermissive]
    _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
    ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tst.cpp:16:5: note:   in call to ‘A::operator B()’
     operator B()
     ^~~~~~~~
tst.cpp:3:7: note: candidate: constexpr B& B::operator=(B&&) <near match>
 class B
       ^
tst.cpp:3:7: note:   conversion of argument 1 would be ill-formed:
In file included from tst.cpp:1:0:
/usr/include/c++/7/tuple:313:19: error: invalid user-defined conversion from ‘const A’ to ‘B&&’ [-fpermissive]
    _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
    ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tst.cpp:16:5: note: candidate is: A::operator B() <near match>
     operator B()
     ^~~~~~~~
tst.cpp:16:5: note:   passing ‘const A*’ as ‘this’ argument discards qualifiers
In file included from tst.cpp:1:0:
/usr/include/c++/7/tuple:313:19: error: passing ‘const A’ as ‘this’ argument discards qualifiers [-fpermissive]
    _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
    ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tst.cpp:16:5: note:   in call to ‘A::operator B()’
     operator B()
     ^~~~~~~~
In file included from tst.cpp:1:0:
/usr/include/c++/7/tuple:313:19: error: conversion to non-const reference type ‘class B&&’ from rvalue of type ‘B’ [-fpermissive]
    _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
    ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Upvotes: 2

Views: 211

Answers (1)

Quentin
Quentin

Reputation: 63124

You're using the copy-assignment operator from std::tuple. It takes a const & parameter, which means that your conversion has to be available from a const object.

Inside class A:

operator B() const
//           ^^^^^

Upvotes: 3

Related Questions