更改类型后丢失对std :: variant对象的引用

问题描述 投票:0回答:1

请注意,可以通过运行以下代码段重现该问题(我在gcc 9.1中使用了wandbox)

所以我有两个定制类型(std::arraystd::variant)的NormalSpecial(为简单起见,大小为2)。Normal被指定为第一类型,因此在构造类时,该数组默认为-用Normal对象构造。我更改了数组第一个元素的一些内部数据成员,并将其打印出来。看起来不错。

现在我想将数组的第二个元素设置为Special对象。我尝试通过分配一个新值并根据本教程(emplace

使用https://www.bfilipek.com/2018/06/variant.html#changing-the-values来完成此操作

但是,当我尝试更改第二个对象(现在键入Special)的内部数据成员时,似乎没有对原始数组中的对象进行操作。打印结果显示构造的默认值(本例中为0)我不熟悉std::variant,所以我不知道为什么会这样。如何获得对数组中最近的类型更改变量对象的实际引用?

#include <iostream>
#include <memory>
#include <cstring>
#include <array>
#include <variant>

struct Normal {
    struct Header {
        std::array<uint8_t, 2> reserved;
    };
    Normal() : frame{0}, payload{reinterpret_cast<uint8_t*>(frame + sizeof(Header))} {}
    constexpr static auto LENGTH = 10;
    uint8_t frame[LENGTH];
    uint8_t* payload;
};

struct Special {
    struct Header {
        std::array<uint8_t, 3> reserved;
    };
    Special() : frame{0}, payload{reinterpret_cast<uint8_t*>(frame + sizeof(Header))} {}
    constexpr static auto LENGTH = 11;
    uint8_t frame[LENGTH];
    uint8_t* payload;
};

std::array<std::variant<Normal, Special>, 2> handlers;
Normal* normal_handler;
Special* special_handler;

int main() {
    auto& nh = std::get<Normal>(handlers[0]);
    memset(nh.payload, 3, 3);
    normal_handler = &nh;

    handlers[1].emplace<1>(Special{});
    auto& sh = std::get<Special>(handlers[1]);
    memset(sh.payload, 4 ,4);
    // memset(std::get<Special>(handlers[1]).payload, 4, 4);
    special_handler = &sh;

    for (int i = 0; i < 10; i++) {
        // Expect 3 bytes from 3rd bytes = 3
        std::cout << (int) normal_handler->frame[i] << " ";
    }

    std::cout << std::endl;

    for (int i = 0; i < 11; i++) {
        // Expect 4 bytes from 4th bytes = 4
        std::cout << (int) special_handler->frame[i] << " ";
        // std::cout << (int) std::get<Special>(handlers[1]).frame[i] << " ";
    }

}
c++ arrays variant
1个回答
0
投票

您的问题与std::variant无关,以下代码显示了相同的行为:

#include <iostream>
#include <memory>
#include <cstring>

struct Special {
    struct Header {
        std::array<uint8_t, 3> reserved;
    };
    Special() : frame{0}, payload{reinterpret_cast<uint8_t*>(frame + sizeof(Header))} {}
    constexpr static auto LENGTH = 11;
    uint8_t frame[LENGTH];
    uint8_t* payload;
};

int main() {

    Special s1;
    s1 = Special{};
    memset(s1.payload, 4 ,4);

    for (int i = 0; i < 11; i++) {
        // Expect 4 bytes from 4th bytes = 4
        std::cout << (int) s1.frame[i] << " ";
    }
}

此行:

    s1 = Special{};

创建一个临时的Special对象,然后将其分配给s1。默认的复制和移动构造函数会在临时位置将s1.payload设置为payload的值。因此,s1.payload是临时对象中指向frame的悬挂指针,因此其余代码具有未定义的行为。

最简单的解决方法是将payload成员更改为一个函数:

#include <iostream>
#include <memory>
#include <cstring>

struct Special {
    struct Header {
        std::array<uint8_t, 3> reserved;
    };
    Special() : frame{0} {}
    constexpr static auto LENGTH = 11;
    uint8_t frame[LENGTH];
    uint8_t* payload() { return &frame[sizeof(Header)]; }
};

int main() {

    Special s1;
    s1 = Special{};
    memset(s1.payload(), 4 ,4);

    for (int i = 0; i < 11; i++) {
        // Expect 4 bytes from 4th bytes = 4
        std::cout << (int) s1.frame[i] << " ";
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.