[LLVMbugs] [Bug 12038] New: Missing call to move constructor in vector::emplace_back

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Feb 20 03:14:52 PST 2012


http://llvm.org/bugs/show_bug.cgi?id=12038

             Bug #: 12038
           Summary: Missing call to move constructor in
                    vector::emplace_back
           Product: libc++
           Version: unspecified
          Platform: Macintosh
        OS/Version: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
        AssignedTo: hhinnant at apple.com
        ReportedBy: jonathan.sauer at gmx.de
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified


The following program compiles with clang r150076 and libc++ r150076 in c++11
mode:

#include <cstdio>
#include <vector>

struct S {
    S() : owner(true) { }
    S(S&& rhs) : owner(rhs.owner) { rhs.owner = false; }
    ~S() { std::printf("Destruct %i\n", owner); }

    bool owner;
};

int main(int, char**)
{
    std::vector<S>  s;
    for (int i = 0; i < 2; ++i)
        s.emplace_back();
}


When run, this outputs:

Destruct 0
Destruct 1
Destruct 1

My interpretation: To put the second object into the vector, a reallocation was
necessary which moved the first object to its new position, transferring
ownership. Then the original first object (now no longer owning) was destroyed.
Afterwards, the vector was destroyed, destroying the two objects with ownership
in it.

When compiled and run with libc++ r150956, running the program outputs:

Destruct 1
Destruct 1
Destruct 1

My interpretation: This time, the call to the move constructor was erroneously
omitted, thus there was no transfer of ownership. When the original first
object was destroyed, it still had ownership.

There seems to be an interaction with the copy constructor, because when adding
the following one ("inspired" by auto_ptr):

  S(const S& rhs) : owner(rhs.owner) { const_cast<S&>(rhs).owner = false; }

this constructor is called, resulting in the same output as with r150076:

Destruct 0
Destruct 1
Destruct 1

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list