C ++ default assignment operator by default cannot be called on a class, even if the class does not have specific constructors

While I myself was familiar with the C ++ 14 specification, I read that if the class does not have an explicitly declared Copy Constructor, copy assignment operator, Move Constructor or Move Assignment Operator, the default implementation should be generated by the compiler.

Consider this empty class for streaming security files:

class ThreadSafeFile
{
    std::mutex m_mutex;
    std::string m_FileName;
    std::ofstream m_File;
};

When I try to move it like this:

ThreadSafeFile file;

ThreadSafeFile file2 = std::move(file);

I get this compilation error:

function "ThreadSafeFile :: ThreadSafeFile (const ThreadSafeFile &)" (declared implicitly) cannot be referenced - this is a remote function

Why is this so?

+4
source share
5

, std::mutex . , , . , std::unique_ptr.

struct ThreadSafeFile {
    std::unique_ptr<std::mutex> m_mutex;
    string m_FileName;
    std::ofstream m_File;
};

T.C , , , , . : ++?

+5

std::mutex ( ), ( ) .

cppreference:

std:: mutex , .

, ( ), , , .

ThreadSafeFile file2 = std::move(file);
//ThreadSafeFile::ThreadSafeFile(ThreadSafeFile&&) fails.

//try Copy con (implicitly):
ThreadSafeFile file2(file); 
//ThreadSafeFile::ThreadSafeFile(const ThreadSafeFile&) fails too.

//Error.

, , .

+2

, MCVE . - , , .

clang :

error: call to implicitly-deleted copy constructor of 'ThreadSafeFile'
ThreadSafeFile DestinationOfMoving = std::move(file);
^                     ~~~~~~~~~~~~~~~

note: copy constructor of 'ThreadSafeFile' is implicitly deleted
because field 'm_mutex' has a deleted copy constructor
std::mutex m_mutex;
^

.../std_mutex.h:97:5: note: 'mutex' has been explicitly marked deleted here
mutex(const mutex&) = delete;
^

:

note: copy constructor of 'ThreadSafeFile' is implicitly deleted
because field 'm_mutex' has a deleted copy constructor
std::mutex m_mutex;
^
+2

, , , .., , , .

std::mutex std::ofstream , , .

However, this does not stop you from creating your own, explicit relocation mechanism that handles the fact that it retired in its members - the compiler cannot create a default for you.

+1
source

gcc says:

note: 'ThreadSafeFile::ThreadSafeFile(ThreadSafeFile&&)' is implicitly deleted because the default definition would be ill-formed:
 class ThreadSafeFile{
       ^
error: use of deleted function 'std::mutex::mutex(const std::mutex&)'

http://en.cppreference.com/w/cpp/language/move_constructor

+1
source

Source: https://habr.com/ru/post/1659729/


All Articles