[英]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. 因此,我想通过用户定义的转换将元组的所有元素转换为其他类型,但是由于转换过程
tuple
内部的某些限定符不匹配,因此代码失败。 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);
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You're using the copy-assignment operator from std::tuple
. 您正在使用
std::tuple
的复制分配运算符 。 It takes a const &
parameter, which means that your conversion has to be available from a const
object. 它带有一个
const &
参数,这意味着必须从const
对象中进行转换。
Inside class A
: 内部
class A
:
operator B() const
// ^^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.