From 6306263352190542634
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,942dd48e323646a4
X-Google-Attributes: gidf78e5,public
From: Francis Glassborow <francis@robinton.demon.co.uk>
Subject: Re: const static member in a class
Date: 2000/05/29
Message-ID: <OjNmU8AeYwL5EwHM@robinton.demon.co.uk>#1/1
X-Deja-AN: 628434417
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
References: <3908CCBC.E02307C@hotmail.com> <jRZNYBDqKCC5Ewpe@robinton.demon.co.uk> <392E5925.6BABB160@gmx.de>
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Trace: mail2news.demon.co.uk 959563442 mail2news:25055 mail2news mail2news.demon.co.uk
Organization: Southfield Microcomputer SS
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
MIME-Version: 1.0
Reply-To: Francis Glassborow <francisG@robinton.demon.co.uk>
Newsgroups: comp.std.c++

In article <392E5925.6BABB160@gmx.de>, Ole Reinartz
<ole.reinartz@gmx.de> writes
>AFAIK in that special case you dont even have to define it for the linker
>somewhere, because it is a constant. Because it will never change it doesnt
>have to have a fixed location. The compiler is even allowed to optimize it
>to a literal constant 

NO, it is only allowed to do this if it can prove that you never use the
address of the storage.  The following code is perfectly legitimate:

int const i = 3;
int const * i_ptr = &i;

The compiler can only optimise away the storage if nothing in the
program will detect this. Of course the compiler can optimise all uses
of the value but that is not the same thing as not providing storage.

As I wrote before, it would take a very meticulous compiler to complain
that you had not defined the storage if you never actually used the
address of such storage. If, in the case in point, you make the static
const  data private it is relatively easy for the compiler to determine
that the address is not used, but if you assume that public static const
data is fine, you just shot the optimiser.

>(like it would do when you would #define m_a 1). Still
>the type savety remains.
>Also I think (didnt look it up in the standard though) that it works for all
>the builtin types.

You really should check the standard. A special rule was allowed for
static const integer values because such values are often wanted as
compile time constants (e.g. for an array size). enums were being used
for named integer literals before this change was made.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




