- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 C 和/或 C++ 中是否有标准(或至少安全)的方法来比较 union 的相等性?我希望按位比较在许多情况下都会有用,不管每个 union 中最后分配的成员;例如,可以保留一个特定的位模式来表示“值未初始化”,并且能够检查 union 是否未初始化而无需指定“事件”成员将很有用。
C++ 中的一个例子(虽然我认为这个概念扩展到使用非成员函数的 C):
union MyData
{
public:
// Assume I'm compiling this on a platform where the size of `int`
// exceeds the size of `char*`, or that I'm using some combination
// if `#ifdef`s or similar to ensure that my numeric type is sufficiently
// large, or that I include an extra member that is known to be
// exactly the size of the larger member, just for the sake of
// comparison.
int int_member;
char* ptr_member;
bool isInitialized() const
{
return (*this != INVALID_VAL);
}
bool operator==(MyData const& rhs)
{
return / * ??? */;
}
private:
constexpr MyData INVALID_VAL { /* ??? */ };
}
// ... later, in client code...
MyData d;
bool initialized{d.isInitialized()}; // false
d.ptr_member = new char[32];
bool initialized{d.isInitialized()}; // true
这里,INVALID_VAL
可以通过将 int_member
设置为最大负整数值来定义,因为这是一个不均匀的值,所以它不会在单词边界上因此极不可能分配给 char*
成员(假设分配通常直接来自 new
)。
operator==
的一种可能实现方式很简单:
return int_member == rhs.int_member;
尽管不知道 int_member
是否是“活跃”成员,但我希望这是安全的,因为我看不出为什么要从 char*
进行静态转换到 int
应该失败或有问题。对吗?
如果这个实现是不安全的,像下面这样的事情应该是可能的(当然在 C 中使用 C 风格的转换):
return static_cast<void*>(*this) == static_cast<void*>(rhs);
...当然,如果 MyData
大于指针的大小,您就必须开始使用 sizeof
来解决这个问题。
有人这样做吗?第一个(更简单的)实现安全吗?有什么理由不这样做吗?
最佳答案
我认为更好的方法是将您的 union 包装在一个类或结构中,并使用枚举字段存储最后访问的成员,例如
class MyData {
enum {
unintialized, int_member, ptr_member
} last_member = unintialized;
union {
int int_member;
char* ptr_member;
} union_fields;
public:
bool isInitialized() const
{
return last_member != unintialized;
}
};
如果你有 C++11,last_member 的类内初始化是有效的,否则只需在默认构造函数中初始化它。
为这两个字段创建访问器并相应地设置last_member
,最好在访问器方法中添加检查以确保只能访问“事件成员”。
关于c++ - union 的平等比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29702754/
我一直在努力理解 Javascript 的相等性。你能告诉我为什么以下行返回 false 吗? ? alert((function a(){}) == (function a(){})) // fal
scala 中有一个特别的问题每次都困扰着我。每次它咬我...它咬得很重... 为什么这一行编译通过 val x = "10" if (x != 10) { print("do somethin
我正在努力解决我网站的问题。 我想制作 与具有宽度 960px 的主体相等.我的代码有什么问题或缺失?对不起,我不能直接在这里发布代码,不允许我这样做。这是我的文件的链接: https://githu
我希望这些实例的原型(prototype)相同,但以下相等性检查的计算结果为 false。 var emp1 = new(EmployeeScope())("John"); var emp2 = ne
Hamcrest 中是否有匹配器来比较集合的相等性?有 contains 和 containsInAnyOrder 但我需要 equals 不绑定(bind)到具体的集合类型。例如。我无法将 Arra
struct Something { union { float k; int n; }; bool isFloat; bool ope
是否有 == 的等价物?但结果是x != NA如果 x不是 NA ? 以下是我想要的,但它很笨重: mapply(identical, vec1, vec2) 最佳答案 只需将“==”替换为 %in%
无论如何,我认为这是个问题。我正在使用一个 RelayCommand,它用两个委托(delegate)装饰一个 ICommand。一个是 _canExecute 的 Predicate,另一个是 _e
假设我有一个界面 interface IFoo{ val foo:String } 我想创建等于IFF的foo字符串匹配的类。 简单的例子: class A(override val foo:
谁能解释一下下面这部分代码的含义: private event UserChangedHandler m_UserChanged; public event UserChangedHandler Us
(为冗长的设置道歉。这里有一个问题,我保证。) 考虑一个类 Node具有在构建时分配的不可变唯一 ID。此 ID 用于在持久化对象图时进行序列化等。例如,当一个对象被反序列化时,它会根据 ID 针对主
Ruby API说: The eql? method returns true if obj and other refer to the same hash key. 我更改了 Object 的哈希
在我的 Nant 脚本中,我想将属性值与已知字符串进行比较。看完Nant Expressions文档 我相信我可以做一个基本的“==”比较来评估为 bool 值。 但是鉴于脚本块: 执行时,
简化示例: 我最近设置了 Single Table Inheritance在 Animal 上模型。 Cat和 Dog是 Animal 的子类. 我有一个 Animal工厂 : factory :an
如何使用 NHibernate 通过以下测试? 我认为只需覆盖实体类中的 Equals 和 GetHashCode 就足以使其按照我希望的方式工作。显然,对于非常微不足道的“点”对象,为相同的坐标保留
我是一名优秀的程序员,十分优秀!