fork download
  1. #include <stdio.h>
  2. #include <map>
  3.  
  4. #define LOCAL_VAR_DECL( type, var_name ) static std::map<void*, type> var_name
  5. #define LOCAL_VAR_INIT( var_name ) if ( var_name.find( this ) == var_name.end() ) var_name[this]
  6. #define LOCAL_VAR_DEF( type, var_name, init ) LOCAL_VAR_DECL( type, var_name ); LOCAL_VAR_INIT( var_name ) = init
  7. #define LOCAL_VAR( var_name ) var_name[this]
  8.  
  9. class A
  10. {
  11. public:
  12. void Func( void )
  13. {
  14. LOCAL_VAR_DEF( int, m_State, 0 );
  15. #define m_State LOCAL_VAR( m_State )
  16.  
  17. switch( m_State )
  18. {
  19. case 0:
  20. {
  21. printf( "m_State = 0\n" );
  22. m_State = 1;
  23. break;
  24. }
  25. case 1:
  26. {
  27. printf( "m_State = 1\n" );
  28. m_State = 0;
  29. break;
  30. }
  31. }
  32. }
  33. void func2( void )
  34. {
  35. /* error: 'm_State' was not declared in this scope
  36. m_State = 0;
  37. */
  38. }
  39. };
  40.  
  41. int main() {
  42. A a;
  43. a.Func();
  44. a.Func();
  45. a.Func();
  46. return 0;
  47. }
Success #stdin #stdout 0s 2864KB
stdin
Standard input is empty
stdout
m_State = 0
m_State = 1
m_State = 0