请注意,可以通过运行以下代码段重现该问题(我在gcc 9.1中使用了wandbox)
所以我有两个定制类型(std::array
和std::variant
)的Normal
的Special
(为简单起见,大小为2)。Normal
被指定为第一类型,因此在构造类时,该数组默认为-用Normal
对象构造。我更改了数组第一个元素的一些内部数据成员,并将其打印出来。看起来不错。
现在我想将数组的第二个元素设置为Special
对象。我尝试通过分配一个新值并根据本教程(emplace
)
但是,当我尝试更改第二个对象(现在键入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] << " ";
}
}
您的问题与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] << " ";
}
}