Is the move constructor of ifsteam implicitly deleted?

前端 未结 2 1288
忘了有多久
忘了有多久 2020-12-06 01:56

I have the following simple class:

class Source
{
public:
    Source() = default;
    Source(Source const&) = delete;
    Source(Source&&) = defa         


        
2条回答
  •  再見小時候
    2020-12-06 02:19

    I realise this answer is a little bit late but to give an unmoveable class move sematics you could write a very simple wrapper class. For example:

    #include 
    
    
    
    template 
    class swap_wrapper
    {
        //! internal buffer to hold object (on heap)
        std::unique_ptr p_obj;
    public:
        //! allows any of objects constructors to be called directly
        template 
        explicit swap_wrapper(Args&&... args)
            :   p_obj(
                         new T( std::forward(args)... )
                         ) {    }
    
        //! swaps pointer value of T object is unchanged therefore this 
        //! function puts no requirement on base class.
    
        //! note p_obj is left as a nullptr so dereferencing will cause
        //! undefined behaviour.
        swap_wrapper (swap_wrapper &&other) noexcept
            : p_obj(nullptr)
        {
            swap(other);
        }
    
        //! swaps pointer like above,
        //! T object is not touched; just the pointer.
        swap_wrapper &operator = (swap_wrapper &&other) noexcept
        {
            swap(other);
            return *this;
        }
    
        //! uses swap member function of std:unique_ptr
        void swap(swap_wrapper &other) noexcept
        {
            std::swap(p_obj, other.p_obj);
        }
    
        //! operators to allow access to stream
        T& operator *() { return *p_obj; }
        T const& operator *() const { return *p_obj; }
    
        T * operator ->() { return p_obj.get(); }
        T const * operator ->() const { return p_obj.get(); }
    
    };
    
    
    //! overload of default swap (calls member function swap)
    template 
    inline void swap(swap_wrapper &one, swap_wrapper &two) noexcept
    { one.swap(two); }
    

    This wrapper can then be returned from functions, passed as an rvalue parameter, etc..

提交回复
热议问题