online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <memory> // Shims to enable shared_from(this) or shared_from(pointer) template <typename T> inline std::shared_ptr<T> shared_from_enabled(std::enable_shared_from_this<T>* base) { return base->shared_from_this(); } template <typename T> inline std::shared_ptr<const T> shared_from_enabled(std::enable_shared_from_this<T> const* base) { return base->shared_from_this(); } template <typename T> inline std::shared_ptr<T> shared_from(T* derived) { return std::static_pointer_cast<T>(shared_from_enabled(derived)); } // Example class Hierarchy struct base : public std::enable_shared_from_this<base> {}; struct derived : public base { auto shared_from_this() { return shared_from(this); } auto shared_from_this() const { return shared_from(this); } }; template <typename X> struct derived_x : public derived { auto shared_from_this() { return shared_from(this); } }; // Compilation test int main() { auto pbase = std::make_shared<base>(); auto pderived = std::make_shared<derived>(); auto pderived_x = std::make_shared<derived_x<int> >(); auto const& const_pderived = *pderived; const_pderived.shared_from_this(); shared_from(&const_pderived); // Can just use the external function std::shared_ptr<base> test1 = pbase->shared_from_this(); std::shared_ptr<derived> test2 = pderived->shared_from_this(); std::shared_ptr<derived_x<int> > test3 = pderived_x->shared_from_this(); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue