Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Policy based smart pointer
//Title of this code #include <iostream> #include <stdexcept> template<typename T> struct FullCheckPolicy { static void Check(T *_p) { if (!_p) { throw std::runtime_error("Null pointer!"); } } }; template<typename T> struct NoCheckPolicy { static void Check(T *_p) { } }; template<typename T, template<typename E> class CheckingPolicy = NoCheckPolicy> class SmartPtr { private: T *p_; const T * getPtr() const { CheckingPolicy<T>::Check(p_); return p_; } T * getPtr() { CheckingPolicy<T>::Check(p_); return p_; } public: SmartPtr(T *p = NULL) : p_(p) { } SmartPtr(const SmartPtr &_ptr) { } template< template <typename F> class ChkPolicy> SmartPtr & operator = (const SmartPtr<T, ChkPolicy> &_other) { p_ = _other.p_; } const T * operator ->() const { return this->getPtr(); } T * operator ->() { return this->getPtr(); } const T & operator *() const { return this->getPtr(); } T & operator *() { return *this->getPtr(); } }; template<typename T> SmartPtr<T> make_smart(const T &val) { SmartPtr<T> ptr = new T(val); return ptr; } int main() { SmartPtr<int, FullCheckPolicy> ptr; std::cout<<*ptr<<'\n'; ptr = make_smart(3); std::cout<<*ptr<<'\n'; *ptr = 4; std::cout<<*ptr<<'\n'; }
run
|
edit
|
history
|
help
0
sort
C++ Solar eclipse program(Shoushi integrated) 1644 - 1785 from Ideone( Date: August 14, 2014 )
Gauss v1.1
substr
Speed
Splitwise Problem - 2
Search in a rotated sorted array
Mirror a binary tree
offsetof
reverse array