lua-users home
lua-l archive

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


On Mon, Jun 26, 2006 at 11:33:37AM +0100, David Jones wrote:
> 
> On 26 Jun 2006, at 09:33, Andreas Stenius wrote:
> 
> >Julien Cugnière skrev:
> >>Anyway, here is a simple way to ensure that your constants are unique
> >>(assuming this legal C):
> >>   static const void* regkey1 = &regkey1;
> >>   static const void* regkey2 = &regkey2;
> >
> >No, &regkey1 is not a constant expression, although the result is  
> >constant.
> 
> Doesn't matter.  In C it's legal (though probably stupid) to take a  
> pointer to a const object and convert it to a pointer to a non-const  
> object.  No idea about C++.  Just one of the many ways in which const  
> is broken and surprises programmers.

Without getting into an extended debate, I'd suggest that anyone reading
this and thinking about not using const get a better understanding of
it.  Remember that it's just another type safety mechanism; it's there
for programmers, not the compiler; like most type safety mechanisms, it
can be overridden, but be sure it makes sense to do so.  Don't expect
const to improve optimization; that's not what it's for.  (And always
compile your C programs in gcc with "-Wwrite-strings", so string constants
are const.  g++ does this by default.)

I'm not sure if the problem here is a bug in Apple's version of GCC
or just an incorrect assumption about pointer uniqueness; that said, I
wonder if simply using eg. "volatile const int i = 1;" would work around
the problem.  It would impact optimization of accessing "i", but since
only the pointer is used, that shouldn't matter; and it may tip the
compiler that it can't merge it with other variables.  If it's a
compiler bug, maybe there's a flag to disable it.

-- 
Glenn Maynard