Last active
May 4, 2016 14:23
-
-
Save qrealka/448bf12d0984611ab939eb7f8b27c8dc to your computer and use it in GitHub Desktop.
simple test for move result from async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <future> | |
#include <iostream> | |
class NonCopyable | |
{ | |
std::unique_ptr<int> payload; | |
NonCopyable(const NonCopyable&) = delete; | |
NonCopyable& operator=(const NonCopyable&) = delete; | |
public: | |
NonCopyable() : payload(new int()) | |
{} | |
NonCopyable(NonCopyable&& other) | |
: payload{other.payload.release()} | |
{ | |
} | |
NonCopyable& operator=(NonCopyable&& other) | |
{ | |
if (this == &other) | |
return *this; | |
payload.reset(other.payload.release()); | |
return *this; | |
} | |
int get() const { return payload ? *payload : 0; } | |
void set(int value) { *payload = value; } | |
}; | |
NonCopyable async_init(int value) | |
{ | |
NonCopyable result; | |
result.set(value); | |
return result; | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
auto test = []() -> NonCopyable | |
{ | |
NonCopyable result; | |
result.set(10); | |
return result; | |
}; | |
auto t = std::async(std::launch::async, async_init, 10); | |
NonCopyable result1{ test() }; | |
NonCopyable result2{ t.get() }; | |
std::cout << result2.get(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment