[LLVMbugs] [Bug 15432] New: Wrong default move constructor for union

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sun Mar 3 08:26:06 PST 2013


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

            Bug ID: 15432
           Summary: Wrong default move constructor for union
           Product: clang
           Version: 3.2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: duvan.llvm at gmail.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

In the code below, the default move constructor for Bug seems to not move the
union. If the line for the copy constructor, defined as "default", for the
class Data is removed (which should be a no-op?), then things seem to work.

(I'm using clang 3.2 on Snow Leopard.)

Reduced testcase:

#include <iostream>
#include <cstdint>

enum Type : uint32_t { Zero  = 0, One = 1, Two = 2, Mask = 3, Rest = 0xfffffffc
};

struct Data {
    explicit constexpr Data(uint32_t data = 0, Type type = Zero) : dataM(data
<< 2 | type) {}
    constexpr Data(Data const& data, Type type) : dataM(data.dataM | type) {}
    Data(Data const&) = default;

    Type type() const { return Type(dataM & Mask); }
    Data remove_type() const { Data copy(*this); copy.dataM &= Rest; return
copy; }
    uint32_t dataM;
};

inline std::ostream& operator<<(std::ostream& stream, Data id) { return stream
<< id.dataM; }

class Bug
{
public:
    Bug(Data id1, Data id2) : data(id1), first(id2, Two) {}
    Bug(Bug&&) = default;

    Type type() const { return first.type(); }
    Data no_1() const { return first.remove_type(); }

    friend std::ostream& operator<<(std::ostream& stream, Bug const& bug)
    {
        switch (bug.type())
        {
        case Zero:
            return stream << "0: " << bug.data.index << " (" << bug.first <<
')' << std::endl ;
        case One:
            return stream << "1: " << bug.no_1();
        case Two:
            return stream << "2: " << bug.no_1() << ' ' << bug.data.second;
        default:
            return stream;
        }
    }
private: 
    union U {
        U() {}
        explicit U(unsigned index) : index(index) {}
        explicit U(Data id) : second(id) {}
        unsigned index;
        Data second;
    }; 

    U data;
    Data first;
};

int main() {
    Bug a(Data(1), Data(2));
    std::cout << a << std::endl;
    Bug b(std::move(a));
    std::cout << b << std::endl;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20130303/07f167d4/attachment.html>


More information about the llvm-bugs mailing list