Topic: Elimination of redundant references
Author: "Ed Brey" <brey@afd.mke.etn.com>
Date: 1999/10/22 Raw View
"Theodore.Papadopoulo" <Theodore.Papadopoulo@sophia.inria.fr> wrote in
message news:380DE43D.782B3DB4@sophia.inria.fr...
> Does anything in the standard prevents a compiler to suppress
> multiple references bound to the same variable ???
> What I'm meaning by this is:
>
> class A {
> int & a1,a2,a3;
> public:
>
> A(int & i):a1(i),a2(i),a3(i) { }
> };
Your example doesn't show multiple references. The line
int & a1,a2,a3;
is equivalent to:
int& a1;
int a2;
int a3;
To write it in on line, use:
int &a1, &a2, &a3;
Or, as is often preferred, don't write it in one line.
An existing post speaks well to the point of your question, so I won't
address it here.
---
[ 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 ]
Author: comeau@panix.com (Greg Comeau)
Date: 1999/10/22 Raw View
In article <380F38C5.A2A268E2@sensor.com> Ron Natalie <ron@sensor.com> writes:
>"Theodore.Papadopoulo" wrote:
>>
>> Does anything in the standard prevents a compiler to suppress
>> multiple references bound to the same variable ???
>> What I'm meaning by this is:
>>
>> class A {
>> int & a1,a2,a3;
>
>This is a the one defect in the Stroustrup-style decorations.
>a2 and a3 are NOT references here. They are regular ints.
>
>Perhaps you meant:
>
> class A {
> int& a1;
> int& a2;
> int& a3;
>( or int &a1,&a2,&a3; ).
>
>In that case (when you really have three references), how would you
>tell if the compiler folded them all into a single one anyhow? Once
>a reference is initialized, it's indistinguishable from the thing it
>refers to.
Which begs that the original question can also be asked
using "all" instead of "multiple". :)
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- NOTE 4.2.42 BETAS NOW AVAILABLE
Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.com ***
---
[ 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 ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/10/22 Raw View
Theodore.Papadopoulo wrote:
> Does anything in the standard prevents a compiler to suppress
> multiple references bound to the same variable ???
> What I'm meaning by this is:
>
> class A {
> int & a1,a2,a3;
I assume you mean int &a1, &a2, &a3;
> public:
>
> A(int & i):a1(i),a2(i),a3(i) { }
> };
>
> Is the compiler allowed to implement this class with a single reference
> to i ???
>
> Agreed this example is stupid, but the same can happen when A is
> defined in terms of many other objects (storing references to some
> common object) and
> for which all methods used in A are inlined (each of these objects
> should also never be returned) . This can easily happen when dealing
> with multiple handles
> to a same object for example.
References aren't objects, so you can't take their
addresses, so you can hardly compare them. The
compiler might do this optimisation, but only if
all the constructors of the class share this
property.
--
Valentin Bonnard
---
[ 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 ]
Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/10/22 Raw View
"Theodore.Papadopoulo" <Theodore.Papadopoulo@sophia.inria.fr> writes:
> Does anything in the standard prevents a compiler to suppress
> multiple references bound to the same variable ???
No. It could even suppress *all* references if it could figure out that
they were always something else:
struct a
{
a &me;
int i;
a() : me(*this) { }
void f() { ++me.i; }
};
Since the compiler can know that me is always *this, it could choose
to elide the reference altogether.
---
[ 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 ]
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/22 Raw View
"Theodore.Papadopoulo" <Theodore.Papadopoulo@sophia.inria.fr> writes:
> Does anything in the standard prevents a compiler to suppress
>multiple references bound to the same variable ???
>What I'm meaning by this is:
>class A {
> int & a1,a2,a3;
>public:
> A(int & i):a1(i),a2(i),a3(i) { }
>};
>Is the compiler allowed to implement this class with a single reference
>to i ???
Yes. A reference is not an object, and is not required to take
up space. You cannot take the address of a reference, and sizeof
a reference yeilds the size of the referent. You can't really
tell whether the reference has an independent existence. (You'd
have to step outside the confines of the standard to find out.)
In practice, references are implemented the same as pointers.
To optimize away the extra references in your example, the
compiler would need to be able to determine that everywhere
in the program the three members were always initialized with
the same referent. I doubt that any compiler would bother
to make the special-case check. It's unlikely that any real
code would have that property.
--
Steve Clamage, stephen.clamage@sun.com
---
[ 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 ]
Author: "Theodore.Papadopoulo" <Theodore.Papadopoulo@sophia.inria.fr>
Date: 1999/10/21 Raw View
Does anything in the standard prevents a compiler to suppress
multiple references bound to the same variable ???
What I'm meaning by this is:
class A {
int & a1,a2,a3;
public:
A(int & i):a1(i),a2(i),a3(i) { }
};
Is the compiler allowed to implement this class with a single reference
to i ???
Agreed this example is stupid, but the same can happen when A is
defined in terms of many other objects (storing references to some
common object) and
for which all methods used in A are inlined (each of these objects
should also never be returned) . This can easily happen when dealing
with multiple handles
to a same object for example.
Theo.
--
--------------------------------------------------------------------------------
Theodore Papadopoulo (papadop@sophia.inria.fr)
Projet Robotvis, INRIA - Sophia Antipolis,
2004, route des Lucioles, BP 93, 06902 Sophia Antipolis Cedex -- FRANCE
Phone: (33) 04 92 38 76 01, Fax: (33) 04 92 38 78 45
--------------------------------------------------------------------------------
---
[ 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 ]
Author: christian.bau@isltd.insignia.com (Christian Bau)
Date: 1999/10/21 Raw View
In article <380DE43D.782B3DB4@sophia.inria.fr>, "Theodore.Papadopoulo"
<Theodore.Papadopoulo@sophia.inria.fr> wrote:
> Does anything in the standard prevents a compiler to suppress
> multiple references bound to the same variable ???
> What I'm meaning by this is:
>
> class A {
> int & a1,a2,a3;
> public:
>
> A(int & i):a1(i),a2(i),a3(i) { }
> };
>
> Is the compiler allowed to implement this class with a single reference
> to i ???
>
> Agreed this example is stupid, but the same can happen when A is
> defined in terms of many other objects (storing references to some
> common object) and
> for which all methods used in A are inlined (each of these objects
> should also never be returned) . This can easily happen when dealing
> with multiple handles
> to a same object for example.
The compiler can do any optimisation it wants, as long as the programmer
cannot tell the difference, that is as long as your program behaves as if
the optimisation is not there.
In your case, assuming for example that a reference requires four bytes,
sizeof (A) would go down from 12 byte to four byte, so printing the size
of an object of class A would give different results with and without this
optimisation. But then there is nothing in the definition of C++ that says
how much storage a class with three references should take (I think), and
I dont think that different classes containing three references are
required to have the same size, so this optimisation should be ok.
---
[ 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 ]
Author: Ron Natalie <ron@sensor.com>
Date: 1999/10/21 Raw View
"Theodore.Papadopoulo" wrote:
>
> Does anything in the standard prevents a compiler to suppress
> multiple references bound to the same variable ???
> What I'm meaning by this is:
>
> class A {
> int & a1,a2,a3;
This is a the one defect in the Stroustrup-style decorations.
a2 and a3 are NOT references here. They are regular ints.
Perhaps you meant:
class A {
int& a1;
int& a2;
int& a3;
( or int &a1,&a2,&a3; ).
In that case (when you really have three references), how would you
tell if the compiler folded them all into a single one anyhow? Once
a reference is initialized, it's indistinguishable from the thing it
refers to.
---
[ 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 ]