Topic: subscripting: lvalue versus value


Author: mtorhan@aol.com (MTorhan)
Date: 1996/06/17
Raw View
Does the language standard force compilers to consider that fetching a
subscripted thing's value is different than changing the subscripted
thing's value?

If the compiler is aware of the following two versions of the []
operator...

Type &operator []( unsigned int );
Type operator[]( unsigned int ) const;

...will it choose the 2nd "const" version only when you're working with a
thing that's declared const?  Or, is it smart enuf to use the 2nd "const"
version anywhere the subscripted thing is mentioned, except when the
subscripted thing is at the left of an assignment operator and we need the
1st version for the lvalue.

Or, I guess I could be forgetting some nuance of the language where an
lvalue is needed elsewhere.


[ 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: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/06/17
Raw View
In article <4q3unj$7jj@newsbf02.news.aol.com> mtorhan@aol.com (MTorhan)
writes:

|> Does the language standard force compilers to consider that fetching a
|> subscripted thing's value is different than changing the subscripted
|> thing's value?

|> If the compiler is aware of the following two versions of the []
|> operator...

|> Type &operator []( unsigned int );
|> Type operator[]( unsigned int ) const;

|> ...will it choose the 2nd "const" version only when you're working with a
|> thing that's declared const?  Or, is it smart enuf to use the 2nd "const"
|> version anywhere the subscripted thing is mentioned, except when the
|> subscripted thing is at the left of an assignment operator and we need the
|> 1st version for the lvalue.

This should be a FAQ.  The version chosen depends uniquely on the
const-ness of the container.

|> Or, I guess I could be forgetting some nuance of the language where an
|> lvalue is needed elsewhere.

This only incidentally has anything to do with lvalues.  An lvalue is
required to take the address, for example.  So if you want to support
"&a[i]", where a is a const object, operator[] should return a const
reference, and not a value.

In practice, I find that for anything more than simple array classes,
operator[] will generally return a proxy, rather than a reference
directly into the class.  I suspect that this experience is partially
dependant on the application domain; that people doing intensive
numerical work, for example, will return real references far more often
than I do in my work.
--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
                -- A la recherche d'une activiti dans une region francophone
---
[ 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: jbuck@Synopsys.COM (Joe Buck)
Date: 1996/06/19
Raw View
mtorhan@aol.com (MTorhan) writes:
>If the compiler is aware of the following two versions of the []
>operator...
>
>Type &operator []( unsigned int );
>Type operator[]( unsigned int ) const;
>
>...will it choose the 2nd "const" version only when you're working with a
>thing that's declared const?

Yes, that is what the rules say.

>Or, is it smart enuf to use the 2nd "const"
>version anywhere the subscripted thing is mentioned, except when the
>subscripted thing is at the left of an assignment operator and we need the
>1st version for the lvalue.

No, it does not do this.  operator[] is not treated as special in any way,
and also C++ doesn't consider how a result is used when it parses an
expression and resolves overloaded operators (other languages, e.g. Ada
and VHDL, can do overloading on how a result is used, but not C++).

The usual way of proceeding when you require different actions for use as
lvalues (e.g. your "array" is a hash table but you only want to create a
new entry if it's being used as an lvalue) is to return a "smart
reference" object rather than a reference.  This is an object of a class
that defines a conversion to Type (for accessing as an "rvalue") as well
as an assignment operator (for accessing as an lvalue).

Discussions on these techniques should be in comp.lang.c++.moderated,
I suppose, not here.


--
-- Joe Buck  <jbuck@synopsys.com> (not speaking for Synopsys, Inc)

Work for something because it is good,
not just because it stands a chance to succeed.    -- Vaclav Havel
---
[ 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
]