std :: vector :: push_back obiekt, którego nie można skopiować, powoduje błąd kompilatora

Dostaję błędy kompilacjig++ (GCC) 4.7.2 ale nie naMSVC-2012 kiedy próbujęstd::vector::push_back nie kopiowalny (prywatny konstruktor kopii), ale ruchomy obiekt. Dla mnie mój przykład wygląda identycznie jak wiele innych przykładów na SO i gdzie indziej. Komunikat o błędzie sprawia, że ​​wygląda to na problem ze strukturą nie będącą „bezpośrednią konstrukcją” - nie wiem, co to oznacza, więc jestem podwójnie niepewny, dlaczego obiekt musi być „bezpośrednio konstruktywny”, aby go odepchnąć.

#include <vector>
#include <memory>

struct MyStruct
{

    MyStruct(std::unique_ptr<int> p);
    MyStruct(MyStruct&& other);
    MyStruct&  operator=(MyStruct&& other);

    std::unique_ptr<int> mP;

private:
            // Non-copyable
    MyStruct(const MyStruct&);
    MyStruct& operator=(const MyStruct& other);
};

int main()
{

    MyStruct s(std::unique_ptr<int>(new int(5)));
    std::vector<MyStruct> v;

    auto other = std::move(s);       // Test it is moveable
    v.push_back(std::move(other));   // Fails to compile

    return 0;
}

Daje błędy

/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/type_traits: In instantiation of ‘struct std::__is_direct_constructible_impl<MyStruct, const MyStruct&>’:
... snip ...
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_vector.h:900:9:   required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = MyStruct; _Alloc = std::allocator<MyStruct>; std::vector<_Tp, _Alloc>::value_type = MyStruct]’
main.cpp:27:33:   required from here
main.cpp:16:5: error: ‘MyStruct::MyStruct(const MyStruct&)’ is private

Proste obejście z różnych odpowiedzi:

Posługiwać sięMyStruct(const MyStruct&) = delete; zamiastprivate ctor włamać sięDziedziczyćboost::noncopyable (lub inna klasa z prywatnym agentem)

questionAnswers(1)

yourAnswerToTheQuestion