C++NEXT episode5 - Surprising UB when using reinterpret_cast

Поділитися
Вставка
  • Опубліковано 26 жов 2024

КОМЕНТАРІ • 5

  • @KobiCohenArazi
    @KobiCohenArazi 2 місяці тому

    Thanks again Alex. Question - I am very familiar with the memcpy approach, using it everywhere in this use case. works well. But it works well with simple types. Non std layout, non trivial etc... - would this work for this types as well. for example, I recently had such use case with atomic_bool from vooid*. The lifetime started elsewhere and it was a Cish callback that has void* params. This needed a cast to atomic_bool. What would your approach be in this case?

    • @cppnext-alexd
      @cppnext-alexd  2 місяці тому +1

      well it depends, something simple as atomic_bool should be a problem for the compiler. something that has pointers inside it, well thats not to bad because that memory is just allocated elsewhere and we need just the access pointer, in that case launder is good enough. If it was created elsewhere but in the same application and not transferred via shared memory or network than launder is good enough as well. but best thing todo is to go to compiler explorer create a trivial example, and see how you compiler will handle it.

    • @KobiCohenArazi
      @KobiCohenArazi 2 місяці тому

      @@cppnext-alexd something as simple as atomic bool should or should not be a problem?

    • @cppnext-alexd
      @cppnext-alexd  2 місяці тому +1

      @@KobiCohenArazi should not,

  • @user-01845
    @user-01845 12 днів тому

    Looks like my previous comment was deleted because of a link to godbolt. I spent some time trying to implement something like std::start_lifetime_as, but so far none of the custom implementation can handle below example: ```int ub_func (int* k, float* f) {*k = 2; *f=.0; return *k; } ``` without the compiler flag -fno-strict_aliasing this will return 2 if called like this ``` int v; std::cout