Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22199

gcc problem with std::optional and packed struct member

$
0
0

I need to use a packed struct for parsing incoming data. I also have an std::optional value that I want to assign the value of one of the struct members. However, it fails. I think I understand the problem, basically making a reference to a variable that is not aligned with the memory width can be a bad thing.

The code example compiles with clang 14, but not with gcc 12. Is it a bug or a "feature"?

#include <cstdint>#include <optional>struct Data {    uint8_t a{};    uint32_t b{};} __attribute__((packed));int main() {    Data t;    std::optional<uint32_t> val{};    val = t.b;  // <<< this failes    val = (decltype(t.b)) t.b;}

clang: https://godbolt.org/z/eWfaqb3a3

gcc: https://godbolt.org/z/or1W5MbdG

I know of the general problems with packed structs. As my target is an embedded device with a x86-64 and the data being parsed comes from an industrial standard bus, I believe I'm safe from that.


Viewing all articles
Browse latest Browse all 22199

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>