From -8855385049187183643
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,905ceaebf6c72db5
X-Google-Attributes: gidf78e5,public
From: jbuck@Synopsys.COM (Joe Buck)
Subject: Re: subscripting: lvalue versus value
Date: 1996/06/19
Message-ID: <4q9f09$943@hermes.synopsys.com>#1/1
X-Deja-AN: 161057811
references: <4q3unj$7jj@newsbf02.news.aol.com>
x-original-date: 19 Jun 1996 17:56:25 GMT
organization: Synopsys Inc., Mountain View, CA 94043-4033
x-auth: PGPMoose V1.1 PGP comp.std.c++
newsgroups: comp.std.c++
originator: austern@isolde.mti.sgi.com


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 
]



