Skip to content

Latest commit

 

History

History
55 lines (39 loc) · 1.02 KB

macros.mkd

File metadata and controls

55 lines (39 loc) · 1.02 KB

Macros

Macros programming relies on extensions such as Typeof, auto, and Statement Expressions.

There are still a couple of problems

  1. Multi-line macros are annoying

  2. Collision of variable names

#define foo(x)              \
({                          \
    __auto_type a = (x);    \
})

int main()
{
    int a = 1;
    foo(a);
}
  1. Semicolons can not appear as part of arguments
#define min(x, y)           \
({  __auto_type _x = (x);   \
    __auto_type _y = (y);   \
    (_x < _y) ? _x : _y;    \
})
  1. No pattern matching of expression

  2. Compound expressions can not be used safely in macros because curly brackets are not parsed:

foo((int[]){ 0, 0 })
  1. Compound expressions can not be used safely inside Statement Expressions

Life time of a compound expressions used in a macro written using a statement expression ends, so can not be returned.

New kind of macros?

#macro foo(x * y)
  mul(x, y)
#endmacro