Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Created December 26, 2017 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belltailjp/ec3249ba935fff7c825a5fc7faae5ab7 to your computer and use it in GitHub Desktop.
Save belltailjp/ec3249ba935fff7c825a5fc7faae5ab7 to your computer and use it in GitHub Desktop.
Just a confirm of whether all the information of optional is preserved or not after memory bytes copy
#include <iostream>
#include <experimental/optional>
int main()
{
std::experimental::optional<int> t;
//t = 100; // comment in/out
unsigned char *p = new unsigned char[sizeof(t)];
// copy memory
std::copy((char*)&t, (char*)&t + sizeof(t), p);
std::experimental::optional<int> t2 = *(std::experimental::optional<int>*)p;
if(t2)
std::cout << *t2 << std::endl;
}
//g++ optional.cpp -std=c++14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment