RE: [Dev-C++] #DEFINE versus cont variables in array inicializations
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@em...> - 2003-05-31 22:33:17
|
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]=20
> Sent: Saturday, May 31, 2003 6:52 PM
> To: Ioannis Vranos
> Cc: 'Dev-C++ Mailing List'
> Subject: RE: [Dev-C++] #DEFINE versus cont variables in array=20
> inicializations
>=20
>=20
> I don't know why gcc doesn't optimize a 'const int &var =3D=20
> <value>' when
> referenced.
I was speaking compiler indepedently. You had said that const references
occupy space while only const variables can be "inlined", and then you
confused me with maps, data files and L1/L@ caches. :) I do not care =
what
the current version of GCC does on this matter, the next version may =
behave
differently or the previous versions or even ports of the same version =
on
different machines.
For built in array sizes only const variables and #define can do the =
job. In
general when I want to define a constant I use a const variable, but if =
I
were space concerned I would use a const reference since references do =
not
have an address and in theory are more easy to be optimised out than =
objects
(the same thing that happens when we pass const references to functions
instead of object copies).
> The only thing I could guess is that they never thought
> it useful to optimize the use of the above declaration, since the
> most common use for a const reference is something like:
>=20
> int var =3D 12;
>=20
> void func(const int& ref) {
> // do something
> }
>=20
> int main() {
> func(var);
> }
>=20
> In this case, the compiler doesn't know that func() is only=20
> called with
> var as parameter, i.e. with a constant value, so it can't optimize.
Yes, but var is "inlined". On the other hand calling func(7), a =
temporary is
created.
> If you think about it, what extra results are you expecting from the
> following declaration? It is basically an unchangeable pointer to an
> unchangeable value. But what improvements should it give to=20
> your program?
>=20
> const int &var =3D 12;
I consider that the compiler could more easily inline it than const int
var=3D12; But anyway in real world programs for PCs, whether it will =
inline it
or not, it doesn't matter.
I disagree(d) with you that a const variable can be inlined while a =
const
reference cannot. Theoretically both can be inlined, system-indepedently
speaking.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|