fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct foo {
  5. foo(const char* file, int line) : FOO_A(*this), FOO_B(*this) {
  6. std::cout << file << ":" << line << '\n';
  7. }
  8.  
  9. template <typename T>
  10. foo& op(T val, const char* str) {
  11. std::cout << str << "=" << val << " ";
  12. return *this;
  13. }
  14. foo& FOO_A;
  15. foo& FOO_B;
  16. };
  17.  
  18. #define FOO_A(x) FOO_OP(x, B)
  19. #define FOO_B(x) FOO_OP(x, A)
  20. #define FOO_OP(x, next) \
  21.   op((x), #x).FOO_ ## next
  22.  
  23. #define FOO\
  24.   if ( (false) )\
  25.   ;\
  26.   else foo(__FILE__, __LINE__).FOO_A
  27.  
  28. int main() {
  29. // recursive macros, wooo
  30. std::string world = "world";
  31. FOO("hello")(world)(40 + 2)("blah")(14.5f)('x');
  32. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
prog.cpp:31
"hello"=hello world=world 40 + 2=42 "blah"=blah 14.5f=14.5 'x'=x