Skip to content

Instantly share code, notes, and snippets.

@mortennobel
Created July 22, 2013 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mortennobel/6053995 to your computer and use it in GitHub Desktop.
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).
//
// 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