Skip to content

Constructor/destructor of optional type called twice #59

@deadem

Description

@deadem

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions