Topic: const initialization and GNU
Author: comeau@panix.com (Greg Comeau)
Date: 1997/05/10 Raw View
Oleg Zabluda wrote:
> Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote:
> : >class A
> : >{
> : > public:
> : > static const int x = 5;
> : > ...
> : >};
> : >
>
> :
> : ... you still need a definition of `X::x', i.e.
>
> : const int A::x;
>
> : somewhere.
>
> I have a question. A compiler is free (I think) not to allocate
> any storage for constants, unless their address is needed, right?
> For example, in
>
> const int a = 5;
> const int b = 3;
> int main(){
> sleep(a);
> const int * pi = &b;
> }
>
> a compiler is free not to allocate any storage for a, but must
> allocate a storage for b.
Yes, but that does not mean that sleep(b) cannot be as if sleep(3).
This has nothing to do with that b has storage. As another example,
doing this:
inline void foo() { i = 99; }
foo(); // A
p = &foo; // B
(*p)(); // C
foo(); // D
does not mean foo cannot be inlined on line D (or even in line C
one might argue!).
> Now, I know that in practice you probably won't notice an error,
> if you skip 'const int A::x;' in the example above, or you'll
> only find out about the error on the link stage.
Not sure what you mean. In that case you _will_ get an error,
unless your compiler system is malfunctioning, so it wouldn't be
wise to not notice it! ;-)
> Now, the question is: did anyone try to formalize these requirements,
> so that 'const int A::x;' becomes optional under well-defined
> conditions. After all, this is a real performance issue, and the one
> that can make the dreadful #define's preferable to a const variables
> under some circumstances.
The "formalization" of the requirements is precisely allowing
the intializer in the class... that nothing else changes is
exactly why it was seen as a productive change. That is A::x
can be used as a named constant (so long as it is accessible et al).
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
Here:comeau@comeaucomputing.com / BIX:comeau or comeau@bix.com / CIS:72331,3421
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/05/10 Raw View
Greg Comeau <comeau@panix.com> wrote:
: Oleg Zabluda wrote:
: > Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote:
: > : >class A
: > : >{
: > : > public:
: > : > static const int x = 5;
: > : > ...
: > : >};
: > : >
: >
: > :
: > : ... you still need a definition of `X::x', i.e.
: >
: > : const int A::x;
: > Now, I know that in practice you probably won't notice an error,
: > if you skip 'const int A::x;' in the example above, or you'll
: > only find out about the error on the link stage.
: Not sure what you mean. In that case you _will_ get an error,
: unless your compiler system is malfunctioning, so it wouldn't be
: wise to not notice it! ;-)
You may define 'const int A::x;' in any translation unit. So there
is no way for a compiler to detect that it's omitted. Only linker
may or may not notice it.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: comeau@panix.com (Greg Comeau)
Date: 1997/05/11 Raw View
In article <5l259q$1fma@r02n01.cac.psu.edu> Oleg Zabluda <zabluda@math.psu.edu> writes:
>Greg Comeau <comeau@panix.com> wrote:
>: Oleg Zabluda wrote:
>: > Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote:
>: > : >class A
>: > : >{
>: > : > public:
>: > : > static const int x = 5;
>: > : ... you still need a definition of `X::x', i.e.
>: >
>: > : const int A::x;
>
>: > Now, I know that in practice you probably won't notice an error,
>: > if you skip 'const int A::x;' in the example above, or you'll
>: > only find out about the error on the link stage.
>
>: Not sure what you mean. In that case you _will_ get an error,
>: unless your compiler system is malfunctioning, so it wouldn't be
>: wise to not notice it! ;-)
>
>You may define 'const int A::x;' in any translation unit. So there
>is no way for a compiler to detect that it's omitted. Only linker
>may or may not notice it.
If your "compiler system" is "conventional" perhaps so.
If it's not, perhaps not.
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
Here:comeau@comeaucomputing.com / BIX:comeau or comeau@bix.com / CIS:72331,3421
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1997/05/08 Raw View
Hi,
Gercken, Pam (pgercken@dsrnet.com) wrote:
: Is the following initialization of constant x non-ANSI standard?
No, it is legal C++.
: class A
: {
: public:
: static const int x = 5;
: ...
: };
: The GNU compiler allows me to do this, but is it ANSI standard?
The GNU compiler is correct in allowing you to initialize static const
objects if they have integral type: This is covered by the forthcoming
ISO C++ standard. It is reasonable to have this possibility because in
some cases you would still have to use either the preprocessor or the
"enum kludge" to get integral constants if this were not possible: If
you do not initialize the value of a static const integral member
together with its declaration, this object cannot be used as a constant
expression. If you do initialize it where you declare it, you can use
it as constant expression: For some applications it is necessary to use
a constant expression, eg. for an array object allocated on the stack.
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I'm looking for an employment - See my homepage for details
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/05/08 Raw View
Gercken, Pam writes:
> Is the following initialization of constant x non-ANSI standard?
> class A
> {
> public:
> static const int x = 5;
> ...
> };
> The GNU compiler allows me to do this, but is it ANSI standard?
CD2 [class.mem] paragraph 4:
4 A member-declarator can contain a constant-initializer only if it
declares a static member (_class.static_) of integral or enumeration
type, see _class.static.data_.
g++ supports other initializers in the class body, but I believe it
complains if command-line switches -ansi and/or -pedantic are issued.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/05/08 Raw View
Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote:
: >class A
: >{
: > public:
: > static const int x = 5;
: > ...
: >};
: >
:
: ... you still need a definition of `X::x', i.e.
: const int A::x;
: somewhere.
I have a question. A compiler is free (I think) not to allocate
any storage for constants, unless their address is needed, right?
For example, in
const int a = 5;
const int b = 3;
int main(){
sleep(a);
const int * pi = &b;
}
a compiler is free not to allocate any storage for a, but must
allocate a storage for b.
Now, I know that in practice you probably won't notice an error,
if you skip 'const int A::x;' in the example above, or you'll
only find out about the error on the link stage.
Now, the question is: did anyone try to formalize these requirements,
so that 'const int A::x;' becomes optional under well-defined
conditions. After all, this is a real performance issue, and the one
that can make the dreadful #define's preferable to a const variables
under some circumstances.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Steve Clamage <stephen.clamage@Eng.Sun.COM>
Date: 1997/05/08 Raw View
Oleg Zabluda wrote:
>
> Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote:
> : >class A
> : >{
> : > public:
> : > static const int x = 5;
> : > ...
> : >};
> : >
>
> :
> : ... you still need a definition of `X::x', i.e.
>
> : const int A::x;
>
> : somewhere.
>
> I have a question. A compiler is free (I think) not to allocate
> any storage for constants, unless their address is needed, right?
> For example, in
>
> const int a = 5;
> const int b = 3;
> int main(){
> sleep(a);
> const int * pi = &b;
> }
>
> a compiler is free not to allocate any storage for a, but must
> allocate a storage for b.
>
> Now, I know that in practice you probably won't notice an error,
> if you skip 'const int A::x;' in the example above, or you'll
> only find out about the error on the link stage.
>
> Now, the question is: did anyone try to formalize these requirements,
> so that 'const int A::x;' becomes optional under well-defined
> conditions. After all, this is a real performance issue, and the one
> that can make the dreadful #define's preferable to a const variables
> under some circumstances.
An object declared const at namespace scope has internal linkage
unless declared extern (draft 7.1.1 Storage class specifiers).
Your example shouldn't result in a link error even if no storage is
allocated for object 'a', since 'a' cannot be referenced by
name from any other translation unit.
The 'x' member of A does not have namespace scope. It is a static
data member of a class, and thus has extern linkage. The compiler is
not allowed to omit storage for extern objects if such an omission
would result in any sort of program failure or change in semantics.
Because 'x' is declared const, the compiler can use its defined value
any place 'x' is referenced. So even if storage is allocated for x, you
do not lose the other benefits of const objects.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/05/14 Raw View
comeau@panix.com (Greg Comeau) writes:
|> Oleg Zabluda wrote:
|> > I have a question. A compiler is free (I think) not to allocate
|> > any storage for constants, unless their address is needed, right?
|> > For example, in
|> >
|> > const int a = 5;
|> > const int b = 3;
|> > int main(){
|> > sleep(a);
|> > const int * pi = &b;
|> > }
|> >
|> > a compiler is free not to allocate any storage for a, but must
|> > allocate a storage for b.
|>
|> Yes, but that does not mean that sleep(b) cannot be as if sleep(3).
|> This has nothing to do with that b has storage.
For that matter, unless pi is actually used in a way that affects
visible behavior, the compiler may eliminate it, and as a result, not
allocate storage for b either.
More generally, the statement that the compiler is free not to allocate
any storage is misleading, in the sense that this is true for any
variable, under the same conditions: the visible behavior must be the
same as if the variable was allocated and constructed. For example,
most compilers I'm familiar with (on RISC machines with a large number
of registers) will not allocate storage for local int's, even if they
are not const; they will simply keep them in a register for as long as
needed.
The critical condition necessary to do this is that the compiler can
"see" all uses of the variable; the advantage of const is that the
compiler has the right to suppose that even the uses it doesn't see will
not change the variable. (And the problem with non-const variables is
not taking the address per se, but letting it or a reference "escape"
from what the compiler is looking at -- typically, the function.)
--
James Kanze home: kanze@gabi-soft.fr +33 (0)1 39 55 85 62
office: kanze@vx.cit.alcatel.fr +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
-- Conseils en informatique industrielle --
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Gercken, Pam" <pgercken@dsrnet.com>
Date: 1997/05/06 Raw View
Is the following initialization of constant x non-ANSI standard?
class A
{
public:
static const int x = 5;
...
};
The GNU compiler allows me to do this, but is it ANSI standard?
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/05/07 Raw View
"Gercken, Pam" <pgercken@dsrnet.com> writes:
>Is the following initialization of constant x non-ANSI standard?
>
>class A
>{
> public:
> static const int x = 5;
> ...
>};
>
>The GNU compiler allows me to do this, but is it ANSI standard?
The code is fine according to the draft, but this is a relatively
new feature. Also note that only integral constants can be initialized
in this way, and that you still need a definition of `X::x', i.e.
const int A::x;
somewhere.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]