Skip to content

emplace does not construct in-place #18

@rwst

Description

@rwst

By debugging I found that calling emplace constructs the object and std::moves it. This is not consistent with std::optional. I'm using a std::vector<nonstd::optional<OBJ>> object named cms and OBJ has a constructor taking three arguments. In this code:

                               if (not cms[index]) {
                                        cms[index].emplace(e, p, m);
                                }

...the destructor of OBJ is called unexpectedly because here:

+ 46 at optional.hpp:763
   760 	    void emplace( Args&&... args )
   761 	    {
   762 	        *this = nullopt;
-> 763 	        initialize( T( std::forward<Args>(args)...) );
   764 	    }
   765 	

OBJ is apparently created and destroyed temporarily.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions