C++ Compiler Error C2280 “attempting to reference a deleted function” in Visual Studio 2013 and 2015

后端 未结 7 1009
误落风尘
误落风尘 2020-11-27 18:03

This snippet is compiled without errors in Visual Studio 2013 (Version 12.0.31101.00 Update 4)

class A
{
public:
   A(){}
   A(A &&){}
};

int main(i         


        
7条回答
  •  时光取名叫无心
    2020-11-27 18:57

    I encountered the same error, just because I had misused std::unique_ptr.

    Note that std::unique_ptr is non-copyable, it is only moveable.

    Here is the wrong demonstration.

    class word;
    class sentence
    {
        public:
            sentence();
            ~sentence();
    
        public:
            // Wrong demonstration, because I pass the parameter by value/copying
            // I should use 'std::shared_ptr< word >' instead.
            sentence(std::initializer_list< std::unique_ptr< word > > sentence);
    };
    

    The following code is taken from MSVC compiler's STL library. We can see that the copy constructor and copy assignment operator of class unique_ptr are deleted explicitly.

        unique_ptr(const unique_ptr&) = delete;
        unique_ptr& operator=(const unique_ptr&) = delete;
    

提交回复
热议问题