jeudi 27 juin 2019

C++ Custom Thread Wrapper with Templatized Constructor causes compile time error for insertion into std::map

I'm writing my own basic game engine and I'm multithreading it. I've created a RawThread object which wraps an std::thread with message queues, etc. I want to map these RawThreads to an id(unsigned int). So I try to insert one into an std::map<unsigned int, RawThread>, but I get a bunch of very ambiguous template errors. If I comment out the map.insert line, then the code compiles just fine, everything works, but I want to be able to insert a RawThread into the map, as tried in main().

I've stared at this code for an hour, checking to see if I got my RawThread constructor template wrong, or if I'm using my references/pointer incorrectly, but I can't see the problem.

#include <map>
#include <utility>
#include <thread>

class RawThread
{
public:

    RawThread(const RawThread& you)
    {
        *this = you;
    }



    ~RawThread()
    {
        delete mThread;
    }

    template<class Function, class... Args>
    RawThread(Function&&f, Args&&... args)
    {       
        mThread = new std::thread(std::forward<Function>(f),         
                                          std::forward<Args>(args)...);
    }

    const RawThread& operator=(const RawThread& in)
    {
        this->mThread = in.mThread;
        return *this;
    }

    void join()
    {
        mThread->join();
    }
    std::thread* mThread;
};



void hello(int x){while(true)++x;}

int main()
{
    int arg = 0;
    RawThread myThread(&hello, arg);
    unsigned int threadId = 0;
    std::map<unsigned int, RawThread> threadMap;
    threadMap.insert(std::make_pair(threadId, myThread));
    return 0;
}

I got a nasty looking error, so I put it on pastebin: https://pastebin.com/9A4wN7kL

Aucun commentaire:

Enregistrer un commentaire