3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gccの_Generic式がclangの挙動と異なる

Last updated at Posted at 2016-04-30

gccの_Generic式がclangの挙動と異なる

C11の_Generic式について

僕は余りC言語に詳しくないので,この挙動に関して詳しい,もしくは説明できる方がいましたらコメントなどで補足してくれると大変助かります.

コメントで言及しましたが,これは規格バグっぽいです.n1930参照

↓のコードをgcc5.3でコンパイルする.オプションはなし.

# include <stdio.h>

enum {a=1,b,c};

int main(void) {
_Generic((char[b]){0}, char[1]:puts("1"), char[2]:puts("2"), char[3]:puts("3"));
}

こんなエラーがでる

main.c: 関数 ‘main’ 内:
main.c:6:10: エラー: ‘_Generic’ selector of type ‘char *’ is not compatible with any association
 _Generic((char[b]){0}, char[1]:puts("1"), char[2]:puts("2"), char[3]:puts("3"));
          ^

clang3.7でコンパイルする.オプションはなし.
通る.しかも正常に実行される.

./a.out
2

enumじゃなくて,例えばsizeof演算子を使ってみる.

# include <stdio.h>

enum {a=1,b,c};

int main(void) {
_Generic((char[sizeof(char)]){0}, char[1]:puts("1"), char[2]:puts("2"), char[3]:puts("3"));
}

エラーがでる

main.c: 関数 ‘main’ 内:
main.c:6:10: エラー: ‘_Generic’ selector of type ‘char *’ is not compatible with any association
 _Generic((char[sizeof(char)]){0}, char[1]:puts("1"), char[2]:puts("2"), char[3]:puts("3"));
          ^

clangはもちろんエラーなんて出ないし正常に実行される.

エラーの文章で一つおかしいと思う所は,(char[sizeof(char)]){0}の型はchar[1]でchar*じゃないんじゃないか?ということ.

本来はclangの挙動になるべきだと思う.

バグかどうか分かんないけど,もしこれがバグならコンパイラのバグなんて初めて踏んだのでちょっと嬉しかったり.

詳しい方のコメントお待ちしております.

3
3
5

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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?