Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

   : m_renderThread(std::move(threadToGive)) {}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading