Created
July 22, 2013 13:54
-
-
Save mortennobel/6053995 to your computer and use it in GitHub Desktop.
c++11 shim (allows you to use a C++11 compiler with the stdlibc++ without C++11 support ... useful when depending on old libraries).
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
// | |
// cpp11shim.h | |
// | |
// Created by Morten Nobel-Jørgensen on 7/19/13. | |
// Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved. | |
// | |
#ifndef __CPP11_SHIM__ | |
#define __CPP11_SHIM__ | |
template <class T> | |
T&& move (T& arg) noexcept { | |
return static_cast<T&&>(arg); | |
} | |
// Note: not complete unique_ptr implementation | |
template <class T> | |
class unique_ptr{ | |
public: | |
unique_ptr():ptr(nullptr){ | |
} | |
unique_ptr(T *t):ptr(t){ | |
} | |
unique_ptr(unique_ptr&& p):ptr(p.ptr){ | |
if (this != &p){ | |
p.ptr = nullptr; | |
} | |
} | |
unique_ptr& operator=(unique_ptr&& other){ | |
if (this != &other){ | |
ptr = other.ptr; | |
other.ptr = nullptr; | |
} | |
} | |
unique_ptr(const unique_ptr&) = delete; | |
unique_ptr& operator=(const unique_ptr&) = delete; | |
~unique_ptr(){ | |
delete ptr; | |
} | |
T* get(){ | |
return ptr; | |
} | |
void reset(T *t){ | |
if (t != ptr){ | |
T *tmp = ptr; | |
ptr = t; | |
delete tmp; | |
} | |
} | |
private: | |
T* ptr; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment