Search code examples
c++constructormove

std::thread in constructor referencing it's deleted copy constructor?


I'm having a surprising amount of trouble with a constructor I'm writing. It's something incrediably basic, but I'm having a bad day because I'm totally stumped.

class RenderThread {
public:
    RenderThread(std::thread && threadToGive) 
        : m_renderThread(threadToGive) {}

private:
    std::thread m_renderThread;
};


int test() {
    std::thread thread;
    RenderThread rt(std::move(thread));
}

My constructor is trying to call std::thread::thread(const std::thread &) which is definitely not my goal, even if it was possible. I want to move the argument threadToGive into m_renderThread, not copy it. What am I doing wrong here?


Solution

  • You have to std::move() the RenderThread constructor's parameter into the constructor of the m_renderThread member:

       : m_renderThread(std::move(threadToGive)) {}