I am writing the constructor for class A below. mutex_array calls the default std::mutex constructor, which is fine. I then want to construct each element of unique_lock_array from each element of std::mutex in the member initializer list. That is, unique_lock_array[i] should be a unique lock for mutex_array[i] before the body of the constructor. Is that possible?
#include <array>
#include <mutex>
class A {
public:
A() : unique_lock_array(/* ??? */) {}
private:
std::array<std::mutex, 10> mutex_array;
std::array<std::unique_lock<std::mutex>, 10> unique_lock_array;
};
You need a helper function but that is easy to do now with an immediately executed lambda expression. That would give you