使用用户定义的转换转换元组

时间:2018-03-20 16:09:57

标签: c++ templates type-conversion tuples

我认为这段代码最能说明我要做的事情:

#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;
}

所以我想通过用户定义的转换将元组的所有元素转换为不同的类型,但由于转换期间tuple内部的一些限定符不匹配,代码会失败。 如何进行此转换?

以下是错误消息:

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);
    ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 个答案:

答案 0 :(得分:3)

您正在使用std::tuple中的copy-assignment operator。它需要const &参数,这意味着您的转化必须来自const对象。

内部class A

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