jeudi 31 décembre 2015

How can I use std::sort with objects that have no copy constructor?

I'm trying to sort a vector containing objects that are not copy constructible or default constructible (but are move constructible), but I get errors about the compiler not being able to find a valid function for swap. I thought that having a move constructor would be enough. What am I missing here?

class MyType {
public:
    MyType(bool a) {}
    MyType(const MyType& that) = delete;
    MyType(MyType&& that) = default;
};

int main(void) {
    vector<MyType> v;
    v.emplace_back(true);
    sort(v.begin(), v.end(), [](MyType const& l, MyType const& r) {
        return true;
    });
}

Aucun commentaire:

Enregistrer un commentaire