Skip to content

Instantly share code, notes, and snippets.

@qrealka
Last active May 4, 2016 14:23
Show Gist options
  • Save qrealka/448bf12d0984611ab939eb7f8b27c8dc to your computer and use it in GitHub Desktop.
Save qrealka/448bf12d0984611ab939eb7f8b27c8dc to your computer and use it in GitHub Desktop.
simple test for move result from async
#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