0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GCC拡張で式指向にbreakとかcontinueとか

Last updated at Posted at 2024-11-01

ZigとかRustみたいに式の中でbreakとかcontinueするマクロ

#define break ({ break; 0; })
#define continue ({ continue; 0; })

void test() {
  for (;;) {
    int a = break;
    // unreachable
  }
  for (int i = 0; i < 5; i++) {
    if (continue) {}
    // unreachable
  }
}

nullチェックみたいな使い方ができる

#define break ({ break; (void *)0; })
#define continue ({ continue; (void *)0 })
#define orelse ?:

void test() {
  for (...) {
    char *p = malloc(100) orelse break;
    ...
  }
}

いちおう文としても書ける

#define break ({ break; 0; })

void test() {
  for (;;) {
    break;
  }
}

returnは括弧が必要になる(ださい)

#define return(x) ({ return x; (void *)0; })
#define orelse ?:

int test() {
  char *p = malloc(100) orelse return(1);
  ...
}
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?