Why can't I move a mutable function that contains a moved future?

Jack Sabbath

I'm basically trying do do this:

using Type = SomeTypeThatIWantToMove;

std::promise<Type> promised_result;
std::future<Type> promised_future = promised_result.get_future();

using Callback = std::function<void()>;

Callback function_which_should_be_movable =
            [this, future_result(std::move(promised_future))]() mutable
{
    this->some_function(future_result.get());   // signature: void some_function(const Type&)
};

using ResultBuilder = std::function<Type(Callback&&)>;

// result_builder is of type ResultBuilder  
Type thingy = result_builder(std::move(function_which_should_be_movable));

MinGW tell me, that the Move-Constructor of function_which_should_be_movable is deleted, because the Copy-constructor of std::future is deleted. However, I don't see why the compiler would attempt to copy the future instead of moving it.

Lingxi

function_which_should_be_movable is of type std::function. According to cppreference:

template< class F > function( F f ); F must meet the requirements of Callable and CopyConstructible.

The lambda expression you try to use to construct an std::function object is not copyable, and hence the problem.

As to why std::function has this requirement, please see this question: Why the initializer of std::function has to be CopyConstructible? (which is asked exactly by myself). Simply put, the type erasure technique used by std::function will instantiate the copy-constructor of F. This happens regardless of whether you, the user of the std::function object, actually used this copy-constructor or not.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I mutate a slice via iterator mutable reference

From Dev

Swift: Why can't I use 'contains(: )"?

From Dev

Moved /etc to home directory, now I can't boot, how do I move the directory back?

From Dev

Why can't I move std::ofstream?

From Dev

Why I can't move an image by JQuery?

From Dev

Why Can't I move an Element of a map?

From Dev

Why is the mutable reference not moved here?

From Dev

Why can't std::ostream be moved?

From Dev

Why can't std::ostream be moved?

From Dev

Why I can't inflate a layout where the layout contains a fragment?

From Dev

Code works on Android and iOS - why can't I move it into a PCL?

From Dev

Why can't I move directly a byte to a 64 bit register?

From Dev

Why can't I move to the alternate file in Vim

From Dev

Why can't I move a file into an NTFS directory mounted RW?

From Dev

Why can't I delete or move these files without sudo?

From Dev

Why can't I make the mountains in the background move in a parallax fashion?

From Dev

Why can't I call my function?

From Dev

Why can't I invoke a function directly?

From Dev

why can't I defne the function first?

From Dev

Why can't I call my function?

From Dev

Why can't I instantiate an object in a function

From Dev

Why can't I replace this function with a lambda

From Dev

Why can't I call this function in React?

From Dev

Can a value be moved by taking a mutable reference?

From Dev

Can a value be moved by taking a mutable reference?

From Dev

Why can't I call a mutable method (and store the result) twice in a row?

From Dev

Why can I not hardlink to a file I don't own even though I can move it?

From Dev

Why Java code doesn't work when moved into a function?

From Dev

Why can't object (.obj) files be moved across platforms?

Related Related

  1. 1

    Why can't I mutate a slice via iterator mutable reference

  2. 2

    Swift: Why can't I use 'contains(: )"?

  3. 3

    Moved /etc to home directory, now I can't boot, how do I move the directory back?

  4. 4

    Why can't I move std::ofstream?

  5. 5

    Why I can't move an image by JQuery?

  6. 6

    Why Can't I move an Element of a map?

  7. 7

    Why is the mutable reference not moved here?

  8. 8

    Why can't std::ostream be moved?

  9. 9

    Why can't std::ostream be moved?

  10. 10

    Why I can't inflate a layout where the layout contains a fragment?

  11. 11

    Code works on Android and iOS - why can't I move it into a PCL?

  12. 12

    Why can't I move directly a byte to a 64 bit register?

  13. 13

    Why can't I move to the alternate file in Vim

  14. 14

    Why can't I move a file into an NTFS directory mounted RW?

  15. 15

    Why can't I delete or move these files without sudo?

  16. 16

    Why can't I make the mountains in the background move in a parallax fashion?

  17. 17

    Why can't I call my function?

  18. 18

    Why can't I invoke a function directly?

  19. 19

    why can't I defne the function first?

  20. 20

    Why can't I call my function?

  21. 21

    Why can't I instantiate an object in a function

  22. 22

    Why can't I replace this function with a lambda

  23. 23

    Why can't I call this function in React?

  24. 24

    Can a value be moved by taking a mutable reference?

  25. 25

    Can a value be moved by taking a mutable reference?

  26. 26

    Why can't I call a mutable method (and store the result) twice in a row?

  27. 27

    Why can I not hardlink to a file I don't own even though I can move it?

  28. 28

    Why Java code doesn't work when moved into a function?

  29. 29

    Why can't object (.obj) files be moved across platforms?

HotTag

Archive