Загрузка...

Is it legal to std::memmove() from one union member to another?

Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: https://www.instagram.com/ky.emrah

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Is it legal to std::memmove() from one union member to another?

This code is an attempt to reinterpret memory contents as a different type without violating strict aliasing rules. It was suggested as an answer to "Using std::memmove to work around strict aliasing?", and it may be worth reading the comments there before answering.
an answer to "Using std::memmove to work around strict aliasing?"
Using std::memmove to work around strict aliasing?
#include cstring
#include cstdint
#include iomanip
#include iostream

struct Parts {
std::uint16_t v[2u];
};

static_assert(sizeof (Parts) == sizeof (std::uint32_t));
static_assert(alignof(Parts) = alignof(std::uint32_t));

int main()
{
union {
std::uint32_t u;
Parts p;
};
u = 0xdeadbeef;
std::clog std::hex u " ~ ";

std::memmove(&p, &u, sizeof u);
std::clog p.v[0] ", " p.v[1] '\n';
}

#include cstring
#include cstdint
#include iomanip
#include iostream

struct Parts {
std::uint16_t v[2u];
};

static_assert(sizeof (Parts) == sizeof (std::uint32_t));
static_assert(alignof(Parts) = alignof(std::uint32_t));

int main()
{
union {
std::uint32_t u;
Parts p;
};
u = 0xdeadbeef;
std::clog std::hex u " ~ ";

std::memmove(&p, &u, sizeof u);
std::clog p.v[0] ", " p.v[1] '\n';
}

I don't believe that the std::memmove() call is legal because class.union.general.2 says that at most one of the non-static data members of an object of union type can be active at any time. But my belief doesn't seem to be shared.
std::memmove()
class.union.general.2
at most one of the non-static data members of an object of union type can be active at any time
Is this legal? Which parts of the standard permit/prohibit this?
Tags: c++,language-lawyer,unions,memmoveSource of the question:
https://stackoverflow.com/questions/79007001

Question and source license information:
https://meta.stackexchange.com/help/licensing
https://stackoverflow.com/

Видео Is it legal to std::memmove() from one union member to another? канала Emrah KAYA
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять