[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: static const int optimization fails in conditional expressions



> On Tue, Dec 11, 2001 at 05:06:31PM -0600, James Jurach wrote:
> > When class members declared as static const int appear in conditional
> > expressions, they are not properly optimized out. The compiler treats them
> > as undefined variables, rather than integer literals.
> 
> That's because they /are/ undefined variables, not integer literals.
> Integer literals are 17, 42, and -3, not Foo::erf, regardless of how
> unchanging Foo::erf may be.

I understood from nm(1) output that, at least in the past, g++ did not
create a symbol for these int's, but rather performed some kind of inline
optimization.

I don't have access to the specifications, but I was pretty sure that
developers were allowed to initialize const static members in class
declarations.  Through google, I find the following statements:

  (from: http://www.google.com/search?q=cache:hhrtMYMBmlk:www.ph.unimelb.edu.au/~ssk/kde/devel/portable.html+ansi+initialization+of+const+static+members&hl=en)

  Actually, initialization of const static members is a new feature. The ANSI
  C++ Draft dated 2 December 1996 allows the following code: 
     class foo {
        static const int value = 42;
     };

Regardless of whether this was removed from the ANSI drafts, g++ is at
least inconsistent.  These variables appear undefined only in conditional
expressions.  The following code compiles cleanly:

-*- begin testexpr.cpp -*-
#include <iostream.h>
 
class Foo {
 public:
  static const int erf = 0;
  static const int foo = 1;
};
 
int main(int argc,char **argv) {
  cout << "foo: " << Foo::foo << endl;
  return 0;
}
-*- end testexpr.cpp -*-

nm(1) output for condexpr.o shows that a symbol "_3Foo.foo" was expected to
be defined in the object file.  No such symbol appears in testexpr.o.

Does anyone have access to the standard who can verify if initialization of
const static members is indeed allowed?

james



Reply to: