Macro function with several lines for the parameter?

后端 未结 6 1025
悲哀的现实
悲哀的现实 2021-01-07 23:09

In C++, I need to defined a macro. That macro would take as parameter a \"block\" of code.

Can we safely use several lines of code as parameter of a macro fu

6条回答
  •  灰色年华
    2021-01-07 23:52

    In C++ you should use a functor! ;)

    struct ninja
    {
      void operator()() const
      {
        int k = AFunction();
        k++;
        AnotherFunction( k );    
      }
    };
    
    template 
    void do_something(Functor const& f)
    {
      f();
    }
    
    template 
    void do_otherthing(Functor const& f)
    {
      f();
    }
    
    int my_function()
    {
      ninja foo;
      do_something(foo);
      do_otherthing(foo);
    }
    

提交回复
热议问题