I have a compilation error when trying to instantiate a std::thread using decltype.I know of an workarround using lambdas, but i find it hard to understand what i am doing wrong. (maybe i am using decltype wrong).These reproduces on MSVC and GCC 8.1
#include<thread>
template<typename T>
class lockBasedQueue
{
private:
std::queue<T> data_queue;
mutable std::mutex m;
std::condition_variable cond_var;
public:
lockBasedQueue() {}
void push(T newValue)
{
std::lock_guard<std::mutex> lk(m);
data_queue.push(std::move(newValue));
cond_var.notify_one();
}
void wait_and_pop(T& value)
{
std::unique_lock<std::mutex> lk(m);
cond_var.wait(lk, [this]() {return data_queue.size() > 0; });
value = std::move(data_queue.front());
data_queue.pop();
}
bool empty() const
{
return false;
}
};
int main()
{
lockBasedQueue<int> q;
std::thread t1(&lockBasedQueue<int>::push, q, 10);
typedef decltype(q) myQueue;
std::thread t2(&myQueue::empty, q);
t1.join();
t2.join();
return 0;
}