c++ macros with memory?

后端 未结 4 1414
Happy的楠姐
Happy的楠姐 2020-12-10 16:32

Is it possible to define macros

write_foo(A);
and
read_foo();

so that:

WRITE_FOO(hello);

code_block_1;

READ_FOO();

code         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 17:13

    this is the sweet spot for tools like Cog that approach the problem of code generation from an intuitive programming approach using python. This is what would look like your code using it

    /*[[[cog
    # definitions ----
    import cog
    Foo = ''
    def WriteFoo( value ):
        Foo = value
    
    def ReadFoo():
        cog.outl(' %s; ' % Foo)
    
    # generation ---
    WriteFoo( 'hello' )
    ]]]*/
    //[[[end]]]
    
    some_code_block;
    
    /*[[[cog 
    ReadFoo() ]]]*/
    hello;
    //[[[end]]]
    
    some_other_blocK;
    
    /*[[[cog 
    WriteFoo( 'world')
    ReadFoo() ]]]*/
    world;
    //[[[end]]]
    
    
    last_block;
    

提交回复
热议问题