gpt4 book ai didi

c++ - 使用用户定义的转换来转换元组

转载 作者:搜寻专家 更新时间:2023-10-31 01:29:43 26 4
gpt4 key购买 nike

我认为这段代码最好地解释了我正在尝试做的事情:

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

最佳答案

您正在使用 copy-assignment operator来自 std::tuple。它采用 const & 参数,这意味着您的转换必须可从 const 对象获得。

A类中:

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

关于c++ - 使用用户定义的转换来转换元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49389096/

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