Created
June 14, 2015 00:07
-
-
Save bagobor/d4b2bc1384cea95ed52a to your computer and use it in GitHub Desktop.
Value/ID Handle class C++11 way
This file contains hidden or 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 <algorithm> | |
#include <utility> | |
#include <iostream> | |
#include <memory> | |
struct Handle { | |
Handle (int val) : m_handl((int*)(void*)val, EmptyDeleter) { } | |
Handle(Handle&& src) : m_handl(std::move(src.m_handl)) {} | |
Handle(const Handle& that) = delete; | |
~Handle() = default; | |
static void EmptyDeleter(int*) {} | |
std::unique_ptr<int, void(*)(int *)> m_handl; | |
int get_value() { return (int)(void*)m_handl.get(); } | |
}; | |
Handle foo() { | |
Handle h{ 51 }; | |
return h; | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
Handle orig(5); | |
Handle copy = foo();// orig; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment