Skip to main content
added 464 characters in body
Source Link
ecatmur
  • 1.7k
  • 10
  • 15

C++

#include <iostream> #define Q(x,y) x ## y #define P(x,y) Q(x, y) #define print S P(s, __LINE__) = struct S { const char *s; S(const char *s): s(s) {} ~S() { std::cout << s;s << std::endl; } }; int main() { print "Line1"; print "Line2"; print "Line3"; print "Line4"; } 

(Local variables are destroyed in reverse order of declaration.)

C++11

#include <iostream> int main() { struct S { void (*f)(); S(void (*f)()): f(f) {} ~S() { f(); } } s[] = { {[](){ std::cout << "Line1" << std::endl; }}, {[](){ std::cout << "Line2" << std::endl; }}, {[](){ std::cout << "Line3" << std::endl; }}, {[](){ std::cout << "Line4" << std::endl; }}, }; } 

(Much the same, but using lambdas and an array data member instead.)

C++

#include <iostream> #define Q(x,y) x ## y #define P(x,y) Q(x, y) #define print S P(s, __LINE__) = struct S { const char *s; S(const char *s): s(s) {} ~S() { std::cout << s; } }; int main() { print "Line1"; print "Line2"; print "Line3"; print "Line4"; } 

(Local variables are destroyed in reverse order of declaration.)

C++

#include <iostream> #define Q(x,y) x ## y #define P(x,y) Q(x, y) #define print S P(s, __LINE__) = struct S { const char *s; S(const char *s): s(s) {} ~S() { std::cout << s << std::endl; } }; int main() { print "Line1"; print "Line2"; print "Line3"; print "Line4"; } 

(Local variables are destroyed in reverse order of declaration.)

C++11

#include <iostream> int main() { struct S { void (*f)(); S(void (*f)()): f(f) {} ~S() { f(); } } s[] = { {[](){ std::cout << "Line1" << std::endl; }}, {[](){ std::cout << "Line2" << std::endl; }}, {[](){ std::cout << "Line3" << std::endl; }}, {[](){ std::cout << "Line4" << std::endl; }}, }; } 

(Much the same, but using lambdas and an array data member instead.)

Source Link
ecatmur
  • 1.7k
  • 10
  • 15

C++

#include <iostream> #define Q(x,y) x ## y #define P(x,y) Q(x, y) #define print S P(s, __LINE__) = struct S { const char *s; S(const char *s): s(s) {} ~S() { std::cout << s; } }; int main() { print "Line1"; print "Line2"; print "Line3"; print "Line4"; } 

(Local variables are destroyed in reverse order of declaration.)