How to deal with noncopyable objects when inserting to containers in C++

后端 未结 2 787
旧时难觅i
旧时难觅i 2021-02-07 18:31

I\'m looking for the best-practice of dealing with non-copyable objects.

I have a mutex class, that obviously should not be copyable. I added a private copy constructor

2条回答
  •  梦如初夏
    2021-02-07 19:10

    Three solutions here:

    1. Use Pointers - The quick fix is to make it a container of pointers - e.g. a shared_ptr.

    That would be the "good" solution if your objects are truly noncopyable, and using other containers is not possible.

    2. Other containers - Alternatively, you could use non-copying containers (that use in-place-construction), however they aren't very common and largely incompatible with STL. (I've tried for a while, but it's simply no good)

    That would be the "god" solution if your objects are truly noncopyable, and using pointers is not possible.

    [edit] With C++13, std::vector allows inplace construction (emplace_back), and can be used for noncopyable objects that do implement move semantics. [/edit]

    3. Fix your copyability - If your class is copyable as such, and the mutex is not, you "simply" need to fix the copy constructor and assignment operator.

    Writing them is a pain, since you usually have to copy & assign all members except the mutex, but that can often be simplified by:

    template 
    struct NeverCopy : public T 
    {
        NeverCopy() {}
        NeverCopy(T const & rhs) {}
    
        NeverCopy & operator=(T const & rhs) { return *this; }
    }
    

    And changing you mutex member to

    NeverCopy m_mutex;
    

    Unfortunately, using that template you lose special constructors of Mutex.

    [edit] Warning: "Fixing" the Copy CTor/asignment often requires you to lock the right hand side on copy construct, and lock both sides on assignment. Unfortunately, there is no way to override the copy ctor/assignment and call the default implementation, so the NeverCopy trick might not work for you without external locking. (There are some other workarounds with their own limitations.)

提交回复
热议问题