Topic: static const members: take 2


Author: moudgill@cs.cornell.edu ( Mayan Moudgill)
Date: Tue, 22 Feb 1994 15:28:58 GMT
Raw View
I had posted this earlier, and got zip response. This means
   1. It didn't make it out there
  (been known to happen).
   2. Been beaten to death earlier
  (should have gotten at least one ``don't you read the group, stupid?''
  flame).
   3. People don't believe this scenario arises in practice
  ( well, its happened to me more than once...and I don't use C++
  *THAT* often)
   4. People don't think it will make a difference in performance
  ( maybe... )

I'd like some kind of response! PLEASE!
:)
Mayan
------------------- PREVIOUS MESSAGE FOLLOWS --------------------------------


Consider the following situation:
 class A {
 public:
     static const double scale;

            double val;

 inline A(double d) { val = d/scale };
 };

 static const double A::scale = 4.0;

 func(double x)
 {
 A  a = 1.0000; // case 1.
 A  b = x;      // case 2.

 }

Hopefully, the division will get optimized away by the compiler in case 1.
In case 2, the compiler should be able to replace the division by some
simpler sequence of operations.


Now, unfortunately, there can be exactly one instance of the declaration
of the const. Which means that, typically, we'd put the class declaration
in a .H file, and the const definition in a .C file somewhere else....
and the optimization is no longer possible....
{ And yes, I know the enum trick for ints....GAG!}

So: proposal: *PLEASE* treat static const just like all other consts,
in that they can be defined multiple times, as long as they are defined
identically.

:)
Mayan