std::optional not behaving correctly with move semantics in C++17
I'm working on a personal project and This might be a silly question, but I'm updating my dependencies and I am encountering an unexpected behavior when trying to use `std::optional` in combination with move semantics in my C++17 project... I have a class that holds an `std::optional<int>` as a member, and I'm trying to move this class instance into another instance of the same class. However, after the move, the `std::optional<int>` in the moved-from instance still seems to hold the value instead of being in an 'empty' state. Here's a minimal example to illustrate the issue: ```cpp #include <iostream> #include <optional> class MyClass { public: std::optional<int> value; MyClass(int val) : value(val) {} MyClass(MyClass&& other) noexcept : value(std::move(other.value)) {} // Move constructor }; int main() { MyClass a(42); MyClass b(std::move(a)); if (a.value) { std::cout << "Value in a after move: " << *a.value << '\n'; } else { std::cout << "Value in a after move is empty.\n"; } if (b.value) { std::cout << "Value in b: " << *b.value << '\n'; } return 0; } ``` When I run the above code, I expect `a.value` to be empty after the move, but instead, it prints `Value in a after move: 42`. I have confirmed that the move constructor of `MyClass` correctly uses `std::move` on `other.value`. I also tried initializing `std::optional` with a default constructor in the move constructor, but it didn't change the output. Is there something I'm missing regarding the behavior of `std::optional` or the move semantics in this context? Any insights would be greatly appreciated. Any examples would be super helpful. Has anyone dealt with something similar? I'm using Cpp stable in this project. Thanks, I really appreciate it! For context: I'm using Cpp on Ubuntu 22.04.