问题描述
长话短说:我想了解为什么下面的代码的最后一行没有使用D::operator B() const
转换运算符,因此在使用g++ -std=c++17 source.cpp
进行编译(使用{{ 1}}成功了。)
错误是:
g++ -std=c++2a deleteme.cpp
代码是:
$ g++ -std=c++17 deleteme.cpp && ./a.out
In file included from /usr/include/c++/10.2.0/cassert:44,from deleteme.cpp:1:
deleteme.cpp: In function ‘int main()’:
deleteme.cpp:19:14: error: no match for ‘operator==’ (operand types are ‘D’ and ‘B’)
19 | assert(d == B{2}); // conversion operator not invoked explicitly errors // LINE
| ~ ^~ ~~~~
| | |
| D B
In file included from /usr/include/c++/10.2.0/utility:70,from deleteme.cpp:2:
/usr/include/c++/10.2.0/bits/stl_pair.h:466:5: note: candidate: ‘template<class _T1,class _T2> constexpr bool std::operator==(const std::pair<_T1,_T2>&,const std::pair<_T1,_T2>&)’
466 | operator==(const pair<_T1,_T2>& __x,const pair<_T1,_T2>& __y)
| ^~~~~~~~
/usr/include/c++/10.2.0/bits/stl_pair.h:466:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/10.2.0/cassert:44,from deleteme.cpp:1:
deleteme.cpp:19:20: note: ‘B’ is not derived from ‘const std::pair<_T1,_T2>’
19 | assert(d == B{2}); // conversion operator not invoked explicitly errors // LINE
|
这个问题是对this的跟进。在那里,我得到了帮助,编写了一个类#include <cassert>
#include <utility>
struct B {
int x;
B(int x) : x(x) {}
bool operator==(B const& other) const { return x == other.x; }
};
struct D : std::pair<B,char*> {
operator B() const { return this->first; }
};
int main() {
B b{1};
D d{std::pair<B,char*>(B{2},(char*)"hello")};
assert((B)d == B{2}); // conversion operator invoked explicitly is fine
assert(d == B{2}); // conversion operator not invoked explicitly errors // LINE
}
,该类的行为类似于一对(因此继承自该类),其Recursive
是first
,第二个是std::array
(请参见链接以获取详细信息。
由于boost::hana::optional<std::vector<...>>
的{{1}}是second
所包含内容的一种“高级”信息,因此在许多地方,我想投射/转换此对象类似于std::pair
的类first
到其std::pair
的类型。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)