1

I am trying to add map entry with a lambda value that move captures the promiseInstance. There is no problem with the lambda function. But while doing unordered_map::emplace(), copy constructor of promise (which is actually a deleted function) is called instead of calling the move constructor. Explicit usage of move semantics does not help.

#include <unordered_map>
#include <future>
#include <functional>

int main() {

        std::promise<int> promiseInstance;
        std::future<int>  futureInstance = promiseInstance.get_future();
        std::unordered_map<uint32_t, std::function<void(const int value)>> handlers;
        auto                        handler = [promise = std::move(promiseInstance)](const int value) mutable { printf("Value is %d\n",value);};
        handlers.emplace(
                std::piecewise_construct,
                std::forward_as_tuple(5),
                std::forward_as_tuple(handler));
}

On compiling the code, I get the following error.

error: use of deleted function ‘std::promise<_Res>::promise(const std::promise<_Res>&) [with _Res = int]’
In file included from emplaceunordered.cpp:3:0:
/usr/include/c++/7/future:1077:7: note: declared here
       promise(const promise&) = delete;
       ^~~~~~~
Boanerges
  • 476
  • 1
  • 5
  • 16

0 Answers0