微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我可以通过 C++ 中的原始偏移量手动访问字段吗?

如何解决我可以通过 C++ 中的原始偏移量手动访问字段吗?

以下代码段是否使用了未定义/未指定/等。行为?

#include <cstddef>
#include <iostream>
#include <string>

class Test {
    std::string s1{"s1"},s2{"s2"};
    std::ptrdiff_t offset = (char*)(&s2) - (char*)(this);
public:
    std::string& get() { return *(std::string*)((char*)(this) + offset); }
};

int main() {
    Test test;
    std::cout << Test{test}.get(); // note the copy
}

offset 的目的是指向 s1s2(在运行时选择)并且不包含用于复制/移动/访问的特殊逻辑。 std::string 这里只是一个非平凡类的例子。

解决方法

您提出的解决方案包含多个与指针算术相关的未定义行为实例。

第一个 (char*)(&s2) - (char*)(this) 是未定义的行为。此表达式由 expr.add#5 控制。由于指针不是 nullptr 并且它们不指向同一数组中的元素,因此行为未定义。

第二个 ((char*)(this) + offset) 是未定义的行为。这次适用的段落是expr.add#4。由于 (char*)(this) 不是数组元素,因此 offset 的唯一合法值是 0。任何其他值都是未定义行为。

但是 C++ 已经提供了解决您所描述的问题所需的工具:pointer to data member。这些指针指向类型的成员,而不是实例的成员。它可以与指向实例的指针(在本例中为 this 指针)结合以获取普通对象指针。

这是您修改为使用指向数据成员 (https://godbolt.org/z/161vT158q) 的指针的示例:

#include <cstddef>
#include <iostream>
#include <string>

class Test {
    std::string s1{"s1"},s2{"s2"};

    // A pointer to an `std::string` member of the type `Test`
    using t_member_pointer = std::string Test::*;

    // Points to `Test::s2`
    t_member_pointer s_ptr = &Test::s2;

public:
    std::string& get() { 
        // Combine the data member pointer with an instance to get an object
        return (this->*s_ptr);
    }
};

int main() {
    Test test1;
    Test test2 = test1;
    std::cout << test2.get(); // note the copy
}

请注意,s_ptr 指向 Test::s2 而不是 this->s2。数据成员指针的值独立于任何实例,它与该类型的任何实例兼容。因此它不需要在复制或移动过程中更正,如果在实例之间简单地按值复制,它将按预期运行。

,

不,两个指针之间的差异仅对来自同一数组的指针有效:

只有指向同一数组元素的指针(包括超过数组末尾的指针)才可以相减。

https://en.cppreference.com/w/cpp/types/ptrdiff_t

这不适用于班级的不同成员。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。