martinmoene / optional-lite

optional lite - A C++17-like optional, a nullable object for C++98, C++11 and later in a single-file header-only library
Boost Software License 1.0
403 stars 45 forks source link

Constructor/destructor of optional type called twice #59

Closed deadem closed 3 years ago

deadem commented 3 years ago

Code to reproduce:

#include <https://raw.githubusercontent.com/martinmoene/optional-lite/master/include/nonstd/optional.hpp>
#include <iostream>

struct Type {
  Type(int) {
    std::cout << "construct" << std::endl;
  }
  Type(Type &&) {
    std::cout << "move" << std::endl;
  }
  ~Type() {
    std::cout << "destruct" << std::endl;
  }
};

int main()
{
  nonstd::optional<Type> t(0);

  return t.has_value();
}

Expected:

construct
destruct

Output:

construct
move
destruct
destruct

Live code: https://godbolt.org/z/5T575q (gcc 7.5 vs gcc latest output)

martinmoene commented 3 years ago

In release v3.4.0