比较以下结构的两个实例,我收到一个错误:
struct MyStruct1 {
Position(const MyStruct2 &_my_struct_2, const int _an_int = -1) :
my_struct_2(_my_struct_2),
an_int(_an_int)
{}
std::string toString() const;
MyStruct2 my_struct_2;
int an_int;
};
Run Code Online (Sandbox Code Playgroud)
错误是:
错误C2678:二进制'==':找不到哪个运算符带有'myproj :: MyStruct1'类型的左手操作数(或者没有可接受的转换)
为什么?
在看到这个问题之后,我首先想到的是定义泛型等价和关系运算符是微不足道的:
#include <cstring>
template<class T>
bool operator==(const T& a, const T& b) {
return std::memcmp(&a, &b, sizeof(T)) == 0;
}
template<class T>
bool operator<(const T& a, const T& b) {
return std::memcmp(&a, &b, sizeof(T)) < 0;
}
Run Code Online (Sandbox Code Playgroud)
using namespace std::rel_ops然后将变得更加有用,因为它的是由运营商的默认实现完全通用的==和<.显然,这不执行成员比较,而是执行逐位比较,就好像类型仅包含POD成员一样.这是不是与C++如何生成的拷贝构造函数,例如,这是完全一致的做执行成员逐一复制.
但我想知道上述实施是否确实安全.结构自然会具有相同的包装,具有相同的类型,但是填充的内容保证是相同的(例如,用零填充)?是否有任何理由或情况不起作用?
我想知道为什么编译器提供默认的复制构造函数.这个想法背后的策略是什么.
提前致谢.
为什么我们在C++中需要这样的运算符?它在现代C++编程中有何用处?任何可以应用它的真实代码示例都会有所帮助.
这个问题旨在了解现实世界中的实际应用,而无需阅读Herb Sutter的罗嗦提议.尽管如此,对提案没有冒犯.
是否可以定义某种可以为结构创建通用可比运算符的模板?
例如,这样的事情可能吗?
struct A
{
int one;
int two;
int three;
};
bool AreEqual()
{
A a {1,2,3};
A b {1,2,3};
return ComparableStruct<A>(a) == ComparableStruct<A>(b);
}
Run Code Online (Sandbox Code Playgroud)
所有这些都是结构的字段比较.您可以假设所有字段都是基本类型或重载operator ==.
我有很多像这样的结构,如果我可以把它放在一个模板或者用于比较的东西而不是为每个结构定义一个运算符==,它将节省我很多时间.谢谢!
看起来这对C++来说是不可能的.我想知道为什么这个被Clas提案投票,如果有人有理由让我们知道!
对于使用基本类型的解决方案,请参阅R Sahu的解决方案.
我有一段代码实现了==重载.我不得不在某个否定的地方使用它.但是!(A == B)对于读者而言,写作似乎并不清楚.所以我实现了这个双重重载.这样做有什么缺点吗?效率明智吗?:
struct Foo{
bool operator==(const Foo& other) const{
.... //Something that sometimes produces "return false"
return true;
}
bool operator!=(const Foo& other) const{
return !(*this == other);
}
}
Run Code Online (Sandbox Code Playgroud)
额外:为什么编译器没有!=使用否定的默认实现==?
我还是 C++ 的新手(一般编程),如果这个问题很愚蠢或被问过很多次,请原谅我。这是问题..假设在同一个类下有两个对象 A 和 B。
例如
class Fruit{
int apple;
int banana;
fruit(int x, int y){
apple=x;
banana=y;
}
}
Fruit A(1,1);
Fruit B(1,1);
Run Code Online (Sandbox Code Playgroud)
如果我想检查对象 A 的内容是否与对象 B 的内容相同,我是否必须比较从 A 到 B 的每个变量,或者
if(Object A == Object B)
return true;
Run Code Online (Sandbox Code Playgroud)
会做这份工作吗?
我经常有一些类提供简单的逐个成员比较:
class ApplicationSettings
{
public:
bool operator==(const ApplicationSettings& other) const;
bool operator!=(const ApplicationSettings& other) const;
private:
SkinType m_ApplicationSkin;
UpdateCheckInterval m_IntervalForUpdateChecks;
bool m_bDockSelectionWidget;
// Add future members to operator==
};
bool ApplicationSettings::operator==(const ApplicationSettings& other) const
{
if (m_ApplicationSkin != other.m_ApplicationSkin)
{
return false;
}
if (m_IntervalForUpdateChecks != other.m_IntervalForUpdateChecks)
{
return false;
}
if (m_bDockSelectionWidget != other.m_bDockSelectionWidget)
{
return false;
}
return true;
}
bool ApplicationSettings::operator!=(const ApplicationSettings& other) const;
{
return ( ! operator==(other));
}
Run Code Online (Sandbox Code Playgroud)
鉴于此时C++没有提供任何构造来生成运算符==,是否有更好的方法来确保未来成员成为比较的一部分,而不是我在数据成员下面添加的注释?
目前,我在尝试在vector. 我定义了一个vector, tests, 集来包含我自己的结构Test。它包含两个变量,one和two。两者都是 的实例Test。
Test不多,它只包含一个int和char。
我定义了一个宏,用于查找对象的实例是否在给定中vector。
当我尝试编译我的代码时,会导致此错误:
In file included from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/stl_algobase.h:71,
from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/char_traits.h:39,
from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/ios:40,
from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/ostream:38,
from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/iostream:39,
from test.cpp:1:
/usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<Test*, std::vector<Test> >; _Value = const Test]':
/usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/stl_algobase.h:1932:14: required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Test*, std::vector<Test> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const Test>]'
/usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/stl_algobase.h:1977:23: required from …Run Code Online (Sandbox Code Playgroud)