c++/6546: static const members optimization failure

Nathan Myers ncm-nospam@cantrip.org
Thu Jun 27 21:17:00 GMT 2002


I thought folks on this list should know about this.  In short, 
the "will-not-fix" below means that on gcc-3.1, as in 3.0, and 
maybe in 3.2 and beyond, in

  struct foo { static const int bar = 3; };
  int f(int i) { return std::min(foo::bar, i); }

the compiler actually generates code to load the static value 
for foo::bar from memory each time (twice, in the code above), 
instead of using the manifest constant 3 in its place.  
Furthermore, it insists on having a int foo::bar defined 
someplace, creating a porting difficulty from other compilers 
that don't.

Better dust off the old "member enum" trick if you hope to get 
compile-time-constant performance with static-const members.
As an alternative, casting the value: 

  int g(int i) { return std::min(int(foo::bar), i); }

seems to work around the problem.

Sigh.

Nathan Myers
ncm at cantrip dot org

On Thu, Jun 27, 2002 at 10:42:43PM -0000, mmitchel@gcc.gnu.org wrote:
> Synopsis: static const members optimization failure
> 
> State-Changed-From-To: analyzed->closed
> State-Changed-By: mmitchel
> State-Changed-When: Thu Jun 27 15:42:43 2002
> State-Changed-Why:
>     Will not fix.
> 
> http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=6546



More information about the Libstdc++ mailing list