简体   繁体   中英

VS 2017 error C2664 map insert attempt

class   Thread
    {
    Sync::TYPE  sync;       // synchronous start/finish
    char        name[256];  // thread name
    thread      handle;     // thread handle
    HANDLE      abort,      // abort event handle
            start;      // start event handle

    public:
    Thread(const char *desc,Sync::TYPE ctrl,void (*fn)(void *),void *arg=NULL);
    Thread(Thread &th);
    Thread(void);
    ~Thread(void);
    HANDLE      AbortHandle(void) const     { return(abort); }
    HANDLE      StartHandle(void) const     { return(start); }
    thread::id  ID(void) const          { return(handle.get_id()); }
    void        Name(const char *value);
    const   char    *Name(void) const       { return(name); }
    void        Sync(Sync::TYPE value)      { sync=value; }
    Sync::TYPE  Sync(void) const        { return(sync); }
    thread      *ThreadHandle(void)     { return(&handle); }
    Thread      &operator=(Thread &th);
    };

class   ThreadList
    {
    map<thread::id,Thread>  TiL;

    public:
    ThreadList(void);
    ~ThreadList(void);
    HANDLE                  AbortHandle(void);
    void                    Clear(void);
    bool                    Close(thread::id id);
    void                    Close(map<thread::id,Thread>::iterator pos);
    map<thread::id,Thread>::iterator    Create(char *name,Sync::TYPE sync,void (*function)(void*),void *arg=NULL);
    map<thread::id,Thread>::iterator    Find(thread::id id)             { return(TiL.find(id)); }
    int                 Size(void) const                { return(TiL.size()); }
    void                    Start(map<thread::id,Thread>::iterator pos);
    HANDLE                  StartHandle(void);
    };

Now this code generates the error:

map<thread::id,Thread>::iterator    ThreadList::Create(char *name,Sync::TYPE sync,void (*function)(void*),void *arg)
{
pair<map<thread::id,Thread>::iterator,bool> result;
thread::id                  id;
Thread                      th(name,sync,function,arg);

id=th.ThreadHandle()->get_id();
**result=TiL.insert(make_pair(id,th));**
return(result.first);
}

The faulty line is in bold. The exact error message is this:

Error C2664: 'std::_Tree_iterator>> std::_Tree>::insert(std::_Tree_const_iterator>>,const std::pair &)': cannot convert argument 1 from 'std::pair' to 'std::pair &&'

What am I doing wrong here?

Kind regards, Vadim.

PS: Sorry, I should have clarified that moment. There are those definitions as well:

Thread::Thread(Thread &th)
{
sync=th.sync;
StrCopy(name,sizeof(name),th.name);
abort=th.abort;
start=th.start;
handle=move(th.handle);
}

Thread  &Thread::operator=(Thread &th)
{
sync=th.sync;
StrCopy(name,sizeof(name),th.name);
abort=th.abort;
start=th.start;
handle=move(th.handle);
return(*this);
}

Without a MCVE it is hard to say for sure, however looking at your example the most obvious thing I see is thread handle in your Thread class. I am assuming that is a std::thread and if so the documentation for std::thread states

No two std::thread objects may represent the same thread of execution; std::thread is not CopyConstructible or CopyAssignable, although it is MoveConstructible and MoveAssignable.

To fix it you will have to implement move operators to ensure that your Thread class can be emplaced into containers.

You will then either be able to std::move(th) into the map container or directly construct it with map.emplace

I would suggest reading articles on when and how to implement copy/move as it is very easy to get these things wrong and the defaults may be good enough anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM