online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <memory> class FooImp { public: void A(int x) { std::cout << "FooImp::A(" << x << ")\n"; } int B(int x) { std::cout << "FooImp::B(" << x << ")\n"; return x * x; } }; class Foo { public: Foo() : _imp{ std::make_unique<FooImp>() } { } void A(int x) { // use a lambda funtion to call _imp->A call_impl([&] { _imp->A(x); }); } int B(int x) { // use a lambda funtion to call _imp->A return call_impl([&] { return _imp->B(x); }); } private: // No MACRO use a function template // see lamdba functions https://en.cppreference.com/w/cpp/language/lambda // decltype(fn()) = return type of function fn template<typename fn_t> auto call_impl(fn_t fn) -> decltype(fn()) // pass a function object (lambda/std::function) { std::cout << "Construct Measure here\n"; if constexpr (std::is_same_v<void, decltype(fn()) >) { fn(); std::cout << "Construct Log here\n"; } else { auto retval = fn(); std::cout << "Construct Log here\n"; return retval; } } std::unique_ptr<FooImp> _imp; // do NOT use raw pointers }; int main() { Foo foo; foo.A(1); std::cout << foo.B(2) << "\n"; 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