I have a std::vector<S>
, where struct S
contains many data members, including a mutex. Some of these members are initialized by the constructor.
I have created a self-contained example below, where I create and add to the vector once the constructor values are available:
#include <vector>
#include <mutex>
struct S
{
S() = default;
S(int a, double b, char c) : a_(a), b_(b), c_(c){}
std::mutex mutex_;
int a_;
double b_;
char c_;
// + Many more class members
};
int main()
{
std::vector<S> vec;
// Later when constructor arguments are known
vec.push_back(S(6, 6.7, 'f'));
}
However, because one of the members is a std::mutex
I cannot perform the assignment:
<source>:19:16: note: copy assignment operator of 'S' is implicitly deleted because field 'mutex_' has a deleted copy assignment operator
I understand the basics why this error exists- you can't copy a mutex.
I'd prefer not having to write a copy constructor because I have a lot of class members and it seems overkill just to allow mutex to be copyable.
Are there any alternative solutions?