http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57557

            Bug ID: 57557
           Summary: can't reference static const int member with
                    optimizations on
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pavel.v.chupin at gmail dot com

Test case:
#include <iostream>
using namespace std;

template<class A, class B> bool Compare_GT(const A &a, const B &b) { if (a > b)
return true; else return false; }

struct A {
   bool foo(int a) {
     Compare_GT(VARIABLE, a);
   }
   private:
     static const int VARIABLE;
};

const int A::VARIABLE = 30;

int main() {
   A *obj;
//   cout << "30>20 ? " << obj->foo(20) << "\n";
//   cout << "30>40 ? " << obj->foo(40) << "\n";
   return !obj->foo(20);
}

Compiled with -O1 and higher produce:
$ g++ -O1 t.cpp; ./a.out ; echo $?
1

000000000040068c <main>:
  40068c:       b8 01 00 00 00          mov    $0x1,%eax
  400691:       c3                      retq

If compiled with -O0 code is correct and test returns 0:
$ g++ -O0 t.cpp; ./a.out ; echo $?
0

Is it a bug or some sort of undefined behavior by standard?

Reply via email to