Skip to content

Variadic macro portability  #207

Description

@BigCheeZ

If you want compatibility with compilers other than GCC then the current use of variadic macros is an issue:

#define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg))

C99 supports variadic macros but it would look like this:

#define EMIT(fun, ...) (comp->emit_method_table->fun(comp->emit, ##__VA_ARGS__))

Notice the removal of 'arg' from the ellipsis and the use of VA_ARGS.

Even with this change the code is still not portable across C99 compliant compilers. The use of '##' to remove the trailing comma when '...' is empty is a GCC specific implementation. In this case you could move to using two separate macros. Example:

#define EMIT(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
#define EMIT_SINGLE(fun) (comp->emit_method_table->fun(comp->emit))

If you wanted to keep one macro then it would still be a pain because you would have to put 'fun' in with the other arguments.

#define EMIT(...) (comp->emit_method_table->__VA_ARGS__)

Activity

  1. dpgeorge commented on Jan 22, 2014

    @dpgeorge
    Member

    Okay, thanks for this. This is a good solution to make it portable.

  2. added a commit that references this issue on Sep 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions