Insert into vector having objects without copy constructor

后端 未结 4 1681
执笔经年
执笔经年 2020-12-08 10:53

I have a class whose copy constructors are explicitly deleted (because A uses pointers internally and I don\'t want to fall into shallow copy pitfalls):

clas         


        
相关标签:
4条回答
  • 2020-12-08 11:28

    The error is not the fault of emplace_back. To put an object in a vector it must be movable or copyable. If you actually run the code with copy constructor implemented you will notice it is never called. This is an entry on cppreference.com
    enter image description here

    What I would do to fix this is implement the move constructor, that makes it compile and I can't see any really drawback to having a move constructor. And as with the cctor the move constructor will not be called in your current code.

    0 讨论(0)
  • 2020-12-08 11:34

    Just want to add to @kayleeFrye_onDeck's answer. I have a near identical situation to theirs, and the exact syntax that works for me (based on the feedback in the comments) is as follows:

    vector< std::unique_ptr<ClassName> > names; // Declare vector of unique_ptrs of the class instance
    
    std::unique_ptr<ClassName> name_ptr = std::make_unique<ClassName>();
    names.push_back(std::move(name_ptr)); // Need to use std::move()
    
    // Now you can access names objects without error:
    names[0]->classMethod();
    
    0 讨论(0)
  • 2020-12-08 11:39

    You should add move constructor - because std::vector::emplace_back may do relocation which requires copy/move constructor. Or just use std::deque.

    LIVE DEMO

    #include <vector>
    #include <deque>
    using namespace std;
    
    struct NoCopyNoMove
    {
        NoCopyNoMove(const NoCopyNoMove&) = delete;
        NoCopyNoMove& operator=(const NoCopyNoMove&) = delete;
        NoCopyNoMove(NoCopyNoMove&&) = delete;
        NoCopyNoMove& operator=(NoCopyNoMove&&) = delete;
    
        NoCopyNoMove(int){};
    };
    
    struct OnlyMove
    {
        OnlyMove(const OnlyMove&) = delete;
        OnlyMove& operator=(const OnlyMove&) = delete;
        OnlyMove(OnlyMove&&) noexcept {}
        OnlyMove& operator=(OnlyMove&&) noexcept {}
    
        OnlyMove(int){};
    };
    
    int main()
    {
        deque<NoCopyNoMove> x;
        x.emplace_back(1);
    
        vector<OnlyMove> y;
        y.emplace_back(1);
    }
    

    § 23.2.3 Table 101 — Optional sequence container operations

    a.emplace_back(args) [...]

    Requires: T shall be EmplaceConstructible into X from args. For vector, T shall also be MoveInsertable into X.

    0 讨论(0)
  • 2020-12-08 11:50

    I ran into this problem with an external library's class. I was getting,

    "Error C2280 ClassName::ClassName(const ClassName &)': attempting to reference a deleted function"

    I'm guessing that the class I was using had deleted its copy constructor. I couldn't add it to any std containers I knew of for my custom derived-class objects, which wrapped their object with some helpers of mine to help with initialization/error checks.

    I worked around this blocker with (risky) pointers.

    Basically, I transitioned to this:

    std::vector<ClassName*> names;
    ClassName name("arg");
    ClassName name_ptr = &name;
    names.push_back(name_ptr);
    

    from this, originally:

    std::vector<ClassName> names;
    ClassName name("arg");
    names.push_back(name);
    

    Interesting to say, this was the first time coding with C++ that I've actually needed to use pointers for non-pointer-specific usage requirements due to no known alternative. That makes me worry that I may missed something fundamental within my own code.

    Maybe there's a better way to do this, but it's not on this question's list of answers yet...

    edit for Caveat:

    I should have mentioned this before, thanks aschepler; if you do this and the container you're using outlives the object, "bang, you're dead."

    0 讨论(0)
提交回复
热议问题