LoginSignup
0
1

More than 3 years have passed since last update.

gccとclangのエラーメッセージに関する所感

Last updated at Posted at 2020-12-15

clangはgccよりエラーメッセージが良いという話を聞いたことがあったのですが、先日なるほどと思う出来事がありました。
エッセンスとしては以下のようなコードをコンパイルしたときのことです。

Foo.cxx
#include <iostream>

void foo(int i)
{
    std::cout << "foo(int)" << std::endl;
}

class Foo
{
public:
    void foo();
};

void Foo::foo()
{
    std::cout << "Foo::foo()" << std::endl;
    foo(0);
}

判るだろ?俺の意図を汲めよ!と思わないでもないですがエラーになります。
g++は

sample.cxx:17:7: error: no matching function for call to ‘Foo::foo(int)’
  foo(0);
       ^
sample.cxx:14:6: note: candidate: void Foo::foo()
 void Foo::foo()
      ^~~
sample.cxx:14:6: note:   candidate expects 0 arguments, 1 provided

と言ってくるのですがclang++だと

sample.cxx:17:2: error: too many arguments to function call, expected 0, have 1; did you mean
      '::foo'?
        foo(0);
        ^~~
        ::foo
sample.cxx:3:6: note: '::foo' declared here
void foo(int i)
     ^

言語仕様上エラーにしなければならないのは同じですが「お前は ::foo と言いたいのか?」と察してくれます。
それにclangは関数呼び出しの始まり(関数名の1文字目)を発生場所として指しているのに対して、gccは終わり(閉じ括弧)を指しています。

バージョンはそれぞれg++ 7.5.0とclang 6.0.0です。

0
1
0

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
1