I´m using a struct to support a list of Windows SOCKET´s:
struct ConnectedSockets{
std::mutex lock;
std::list<SOCKET> sockets;
};
When i try to compile this (Visual Studio 2012) i get the following error:
"Error C2248:
std::mutex::operator =
cannot access 'private' member declared in class'std::mutex'
"
Does anybody know how to fix this?
A std::mutex
is not copyable, so you will need to implement the operator=
for ConnectedScokets
yourself.
I presume you want to keep a mutex
per instance of ConnectedSockets
, so this should be enough:
ConnectedSockets& operator =( ConnectedSockets const& other )
{
// keep my own mutex
sockets = other.sockets; // maybe sync this?
return *this;
}
Root cause has nothing about sockets. std::mutex
is not copyable, so compiler fails to generate default assignment operator (which is memberwise) for ConnectedSockets
struct. You need to instruct compiler to delete assignment operator (declare it using = delete
specifier) or implement it manually, e.g. ignoring mutex copying.
// To make ConnectedSockets explicitly non-copyable and get more reasonable
// compiler error in case of misuse
struct ConnectedSockets
{
std::mutex lock;
std::list<SOCKET> sockets;
ConnectedSockets& operator=(const ConnectedSockets&) = delete;
};
// To make ConnectedSockets copyable but keep mutex member intact
struct ConnectedSockets
{
std::mutex lock;
std::list<SOCKET> sockets;
ConnectedSockets& operator=(const ConnectedSockets& i_rhs)
{
// Need locking? Looks like it can be problem here because
// mutex:lock() is not const and locking i_rhs wouldn't be so easy
sockets = i_rhs.sockets;
return *this;
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With