auto_destruct.cpp (1019B)
1 #include "osh.h" 2 3 using namespace osh; 4 5 struct V1 { 6 float x; 7 8 V1(float x = 0) : x(x) {} 9 10 V1 operator+(V1 that) { 11 return {x + that.x}; 12 } 13 14 void destruct() { 15 println("V1 destructed: ", x); 16 } 17 }; 18 19 int main() { 20 { 21 StringBuffer a = "Hello"; 22 auto destroyA = autoDestruct(a); 23 24 println("AutoDestruct created: ", a); 25 26 StringBuffer b = a; 27 b.format(", World!"); 28 println("AutoDestruct shallow copied and used: ", b); 29 } 30 println("AutoDestruct destructed"); 31 32 V1 v; // Not affected by the below 33 { 34 typedef AutoDestruct<V1> V1; // Enable AutoDestruct for the whole scope (c++'s default behavior) 35 println("\nUsing AutoDestruct in arithmetic expressions:"); 36 V1 result = V1(10) + V1(20); 37 println("Result: ", result.x); 38 39 v = V1(2494) + result; 40 } 41 V1(50); // Not affected by the above 42 43 println("v has the value: ", v.x); 44 45 return 0; 46 } 47 48 #define OSH_H_IMPLEMENTATION 49 #include "osh.h"