Advertisement
momo5502

MSVC alignas bug

May 19th, 2020
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <cstdint>
  2. #include <cstdio>
  3. #include <functional>
  4.  
  5. class TestClass {
  6. public:
  7.   void* self = nullptr;
  8.  
  9.   // This works
  10.   // std::aligned_storage<sizeof(uint64_t), sizeof(uint64_t)>::type data;
  11.  
  12.   // This breaks
  13.   alignas(uint64_t) char buffer[sizeof(uint64_t)];
  14.  
  15.   TestClass() { self = this; }
  16.   ~TestClass()
  17.   {
  18.     if (self != (void*)this) {
  19.       // If this is reached, the object was moved using a simple memcpy, not using the constructors
  20.       printf("Bug!");
  21.     }
  22.   }
  23.   TestClass(const TestClass& o)
  24.     : TestClass()
  25.   {
  26.   }
  27.  
  28.   TestClass(TestClass&& o)
  29.     : TestClass()
  30.   {
  31.   }
  32.  
  33.   TestClass& operator=(TestClass&&) noexcept { return *this; }
  34.   TestClass& operator=(const TestClass&) noexcept { return *this; }
  35. };
  36.  
  37. std::function<void*()> testLambda()
  38. {
  39.   const TestClass test{};
  40.   return [=]() { return test.self; };
  41. }
  42.  
  43. int main()
  44. {
  45.     testLambda();
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement