Table of Contents
This survey intends to provide a quick reference document that compares various C++ diagnostic output for common mistakes and programming errors.
The following C++ compilers have been chosen for the survey, along with any command line arguments used to produce the output listed in the examples below.
g++ (GCC) 4.5.0 20100328 (experimental)
g++ -S -g -O2 -std=gnu++0x -Wall -Wextra -Wwrite-strings -Wcast-qual -fdiagnostics-show-location=once
icpc -S -g -O2 -Wall -Wcheck -diag-enable vec -diag-enable par -diag-enable openmp -diag-enable sc-include
clang version 1.5 (trunk 99454)
clang++ -S -g -O2 -Wall -fcaret-diagnostics -fshow-source-location -fdiagnostics-print-source-range-info -fdiagnostics-fixit-info
Five diagnostics, theme of missing or extra semicolons.
Table 1. class-semicolon-1.cc
1 // extra semicolon from declaration/definition refactoring class
2 class sly1;
3 {
4 int i;
5 };
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 2. class-semicolon-2.cc
1 // forgotton semicolon at end of class
2 class sly2
3 {
4 int i;
5 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 3. class-semicolon-3.cc
1 // forgotton semicolon at end of member data
2 class sly3
3 {
4 int i
5 void set_it3(int __i) { i = __i; }
6 };
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 4. class-semicolon-4.cc
1 // forgotton semicolon at end of member function
2 class sly4
3 {
4 int i;
5 void set_it4(int __i) { i = __i }
6 };
| ||
|---|---|---|
| gcc | icc | clang |
| | |
One diagnostic, theme of member functions.
Table 5. member-function-1.cc
1 // extra definition from inline refactoring member function
2 class sly1
3 {
4 int i;
5
6 void set_it1(int __i)
7 { int j = __i; }
8 };
9
10 void sly1::set_it1(int __i)
11 { int j = __i; }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Three diagnostics, theme of functions.
Table 6. function-1.cc
1 // forget return type on function
2 foo1();
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 7. function-2.cc
1 // function parameter mismatch, over
2 int foo2();
3 int foo2sub() { return foo2(int);}
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 8. function-3.cc
1 // function parameter mismatch, under
2 int foo3(int);
3 int foo3sub() { return foo3();}
4
| ||
|---|---|---|
| gcc | icc | clang |
| | |
One diagnostic, theme of templates.
Table 9. template-instantiate-1.cc
1 template <class T>
2 struct A
3 {
4 typename T::Type t;
5 };
6
7 typedef int MYT;
8
9 A<MYT> a;
10
11 struct B { typedef int Type; };
12
13 int main()
14 {
15 A<B>() + 1;
16 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Three diagnostics, theme of examples from GCC bugzilla
Table 10. 9335.cc
1 template <int N> struct X {
2 static const int value = X<N-1>::value;
3 };
4 template struct X<1000>;
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 11. 10118.cc
1 struct A { void foo(); };
2 struct B : A {};
3 typedef void (B::*B_ptr) ();
4
5 template <class T, void (T::* U)()> struct CT {};
6
7 CT<B, (B_ptr) &A::foo> c;
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 12. 14263.cc
1
2 struct A { };
3
4 struct B : virtual A { };
5
6 int main()
7 {
8 A* p = new B;
9 B* q = static_cast<B*>(p); // #1
10 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 13. 14283.cc
1 template<class T>
2 class Class1;
3
4 class Class2 {
5 } // no ;
6
7 typedef Class1<Class2> Type1;
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 14. 15538.cc
1
2 template <typename D>
3 struct A { typename D::Q r;};
4
5 template<typename G>
6 struct H : A<H<G> > { typedef G* Q; };
7
8 H<bool> h;
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 15. 16128.cc
1 template<typename T>
2 struct A {
3 };
4
5 namespace H {
6 template<typename T>
7 struct B {};
8 };
9
10 A a;
11 H::B b;
12
13 int main() {
14 A a;
15 H::B b;
16 return 0;
17 }
18
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 16. 16232.cc
1 struct A
2 {
3 A() { Foo(); }
4 template<typename U> void Foo();
5 };
6
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 17. 16582.cc
1 template<class T, char* p> struct X {
2 X();
3 X(const char* q) { /* ... */ }
4 };
5
6 char p[] = "Vivisectionist";
7 X<int, p> x2; // OK
8 X<int, &p[0]> x3; // ERROR
9
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 18. 17166.cc
1 struct A {};
2 int foo(A);
3 template <typename T> int foo(void *, T);
4 enum {e};
5 int i = foo(0, e);
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 19. 19377.cc
1 class A {
2 protected:
3 int i;
4 };
5
6 class B : public A {
7 private:
8 using A::i;
9 };
10
11 class C : public B {
12 public:
13 void f() { A::i = 0; }
14 };
15
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 20. 19502.cc
1 template<typename T> struct A
2 {
3 template<T> struct B;
4 };
5
6 A<void> a;
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 21. 19538.cc
1 struct A { };
2 typedef struct A A;
3 struct A a; // [1]
4
5 struct wrapper {
6 struct B { };
7 typedef struct B B;
8 struct B b; // [2]
9 };
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 22. 20332.cc
1 struct bar {};
2 void foo1() {
3 bar& b = bar();
4 }
5 void foo(bar& b = bar()) {}
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 23. 20397.cc
1 class B {
2 public:
3 int mi; // nonstatic member
4 static int si; // static member
5 };
6
7 class D : private B {
8 };
9
10 class DD : public D {
11 void f();
12 };
13
14 void DD::f() {
15 mi = 3; // error: mi is private in D
16 si = 3; // error: si is private in D
17 B b;
18 b.mi = 3; // OK (b.mi is different from this->mi)
19 b.si = 3; // OK (b.si is different from this->si)
20 B::si = 3; // OK
21 B* bp1 = this; // error: B is a private base class
22 B* bp2 = (B*)this; // OK with cast
23 bp2->mi = 3; // OK: access through a pointer to B.
24 }
25
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 24. 21038.cc
1 void foo() {
2
3 int i;
4 int j;
5 int k;
6
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 25. 21386.cc
1 int i() { return 0; }
2 class A {};
3 A a() { return A(); }
4 int main()
5 {
6 int * pi = &i();
7 A * pa = &a();
8 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 26. 21413.cc
1 template <typename TT> struct a
2 {
3 typedef typename TT::value_type value_type;
4 };
5
6 template<typename T> void foo(int);
7 template<typename C> typename a<C>::value_type foo(const C&);
8 int main() {
9 int s;
10 foo<float>(s);
11 }
12
13
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 27. 21631.cc
1 int f(int&);
2 int f();
3 int g(void)
4 {
5 return f(1);
6 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 28. 24375.cc
1 template<typename T> class foo;
2
3 template<>
4 struct foo<int> { static const int u = 0; static int v; };
5
6 const int foo<int>::u;
7
8 const int foo<int>::u;
9
10 int foo<int>::v;
11
12 int foo<int>::v;
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 29. 24591.cc
1 template<typename T> class A
2 public:
3 int i;
4 };
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 30. 24607.cc
1 template<typename T> class A{};
2 template<typename T> class B{};
3 template<typename T> class C{};
4 template<typename T> class D{};
5 template<typename T> class E{};
6 template<typename T> void f() {
7 typedef A<typename B<D<E<int> > > > foo;
8 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 31. 24985.cc
1 void bar(void)
2 {
3 _Complex float Gamma;
4 int *P;
5 P = (P-42) + Gamma*4;
6 }
7
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 32. 25666.cc
1 struct A { ~A(); };
2
3 struct B
4 {
5 template<int> friend A::~A();
6 };
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 33. 25689.cc
1 struct Var {
2 operator int() const;
3 Var& operator = (int);
4 bool operator == (int);
5 };
6 bool operator == (int, Var&);
7
8 void foo(Var var)
9 {
10 if (var = 0); // if (var.operator=(0))
11 }
12
13 [ foo.cpp.t03.gimple ]
14
15 void foo(Var) (var)
16 {
17 bool retval.0;
18 struct Var & D.1768;
19 int D.1769;
20
21 D.1768 = operator= (&var, 0);
22 D.1769 = operator 1 (D.1768);
23 retval.0 = D.1769 != 0;
24 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 34. 25733.cc
1 int bar(int a, int b, int c)
2 {
3 if ((b = c) && (a == 0))
4 {
5 return 0;
6 }
7 return 1;
8 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 35. 25801.cc
1 extern int (*a)[];
2 void g(void) { a++; }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 36. 26950.cc
1 namespace N {
2 const int a = 42;
3 enum N { e0 = N::a };
4 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 37. 34758.cc
1 struct A
2 {
3 A (const A& = A());
4 };
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 38. 35112.cc
1 namespace X { struct A; }
2 namespace Y { struct A; }
3 namespace Z { struct A; }
4
5 using namespace X;
6 using namespace Y;
7 using namespace Z;
8
9 A* p;
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 39. 35335.cc
1 struct A {};
2
3 void foo()
4 {
5 A a;
6 a = ({ { 1; } });
7 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 40. 35441.cc
1 void foo1(char **p, char **q)
2 {
3 (p - q)();
4 }
5
6 void foo2(char *p, char *q)
7 {
8 (p < q ? p : q)();
9 }
10
11
12 void foo3(unsigned i, int j)
13 {
14 (i << j | i >> (32 - j))();
15 (i >> j | i << (32 - j))();
16 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 41. 35742.cc
1 void foo()
2 {
3 for (;;)
4 ({break;})();
5 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 42. 38612.cc
1 struct Base {};
2 struct X; // X derived from Base later but incomplete
3 here
4 struct Y {}; // Y not derived from Base
5
6 int test1(int Base::* p2m, X* object)
7 {
8 return object->*p2m; // fails because X is incomplete
9 }
10
11 struct X : Base
12 {
13 };
14
15 int test2(int Base::* p2m, X* object)
16 {
17 return object->*p2m; // OK
18 }
19
20 int test3(int Base::* p2m, Y* object)
21 {
22 return object->*p2m; // fails because Y is not derived from A
23 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 43. 39478.cc
1 template< typename T >
2 struct foo
3 {
4 typename T::type dummy();
5 };
6
7 template< typename T >
8 struct bar
9 {
10 typedef void type;
11 foo< bar > p;
12 };
13
14 foo< bar< int > > x;
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 44. 39728.cc
1 # include <fstream>
2 # include <istream>
3
4 using namespace std;
5
6 ifstream x;
7 ifstream& y = x;
8
9 int main(int argc, char** argv) {
10 y = x;
11 return 0;
12 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 45. 39936.cc
1 #include <vector>
2
3 struct A {
4 double time;
5 size_t id;
6
7 A() { } // uninitialized
8
9 };
10
11 struct B {
12 typedef std::vector<A> array_type;
13 array_type array;
14
15 void
16 foo(void);
17 };
18
19 void
20 B::foo(void) {
21 array.resize(1);
22 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 46. 40261.cc
1 template <class> struct A { };
2 template <class T, class U = A<T> > struct B: U { void foo (); };
3
4 template <class T> void B<T>::foo () { }
5
6 int main ()
7 {
8 B<int> b;
9 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 47. 41796.cc
1 struct B1 {
2 void f();
3 static void f(int);
4 int i;
5 };
6 struct B2 {
7 void f(double);
8 };
9 struct I1: B1 { };
10 struct I2: B1 { };
11 struct D: I1, I2, B2 {
12 using B1::f;
13 using B2::f;
14 void g() {
15 f(); // Ambiguous conversion of this
16 f(0); // Unambiguous (static)
17 f(0.0); // Unambiguous (only one B2)
18 int B1::* mpB1 = &D::i; // Unambiguous - G++ gets this one wrong
19 int D::* mpD = &D::i; // Ambiguous conversion
20 }
21 };
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 48. 41895.cc
1 float
2 foo(void)
3 {
4 _Complex float f;
5 __imag__ f = 0;
6 return f;
7 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 49. 43076.cc
1 struct S;
2 template < typename > struct T
3 {
4 template < typename >
5 template < bool > struct T < S >
6 {
7 void f () {
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 50. 43113.cc
1 template < typename >
2 struct A {
3 struct S;
4 A < S > ht;
5 };
6
7 struct B : A < B > {};
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 51. 43253.cc
1 #include <cstdlib>
2 int main() {
3 int *x = new int();
4 x->~int();
5 exit(0);
6 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |
Table 52. 43431.cc
1 typedef long int intptr_t;
2
3 void ff_ac3_downmix_c(float (*samples)[256], float (*matrix)[2], int out_ch,
4 int in_ch, int len)
5 {
6 int i, j;
7 float v0, v1;
8 if(out_ch == 2) {
9 for(i=0; i<len; i++) {
10 v0 = v1 = 0.0f;
11 for(j=0; j<in_ch; j++) {
12 v0 += samples[j][i] * matrix[j][0];
13 v1 += samples[j][i] * matrix[j][1];
14 }
15 samples[0][i] = v0;
16 samples[1][i] = v1;
17 }
18 } else if(out_ch == 1) {
19 for(i=0; i<len; i++) {
20 v0 = 0.0f;
21 for(j=0; j<in_ch; j++)
22 v0 += samples[j][i] * matrix[j][0];
23 samples[0][i] = v0;
24 }
25 }
26 }
| ||
|---|---|---|
| gcc | icc | clang |
| | |