🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

9 total messages Started by Tony Cook Fri, 25 Aug 1995 06:13
iterators and ->
#1845
Author: Tony Cook
Date: Fri, 25 Aug 1995 06:13
27 lines
1238 bytes
Why don't iterators support the '->' operator - which would be more
convenient than '(*i).'?

I know this has been asked before - but the response has usually been
that -> won't work where the container is an 'int'.

But:
===
  14.3.3  Instantiation of operator->                       [temp.opref]

1 If  a  template  class  has  an operator->, that operator-> can have a
  return type that cannot be dereferenced by -> as long as  that  opera-
  tor->  is  neither  invoked, nor has its address taken, isn't virtual,
  nor is explicitly instantiated.
===

Is there some other reason?
-- 
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Implicit operation relationships
#2342
Author: J Scott Peter XXXIII I/III
Date: Sun, 29 Oct 1995 19:51
70 lines
3483 bytes
According to Tony Cook <tony@online.tmx.com.au>:
>Why don't iterators support the '->' operator - which would be more
>convenient than '(*i).'?

>I know this has been asked before - but the response has usually been
>that -> won't work where the container is an 'int'.

What C++ really needs is implicit relationships between operators, which
could greatly simplify programming.  Example: In C, "a->b" is semantically
identical to "(*a).b".  This relationship should be true in C++ as well.
Why the hell when I define operator* (unary) should I also have to define
operator-> ?  

In fact, there's a whole list of operator relationships that compilers
should infer when an operator is used that has no definition, but a related
operator is defined:

a += b		=>  a = a + b
a + b		=>  tmp = a; a + b; tmp
Similarly for other composite operators.

a != b		=> !(a == b)	and vice versa
a >= b		=> !(a < b)	etc
a < b		=> a - b < 0	etc
a - b		=> a + (-b)	and vice versa
-b		=> 0 - b
+b		=> b
a++		=> a += 1	also --
a->b		=>  (*a).b

One obvious benefit of this is reduced verbosity.  For example, most classes
will need to define only composite operators, or non-composite operators;
the other category will take care of itself.  if your class is amenable to
it, you only need to define a single numeric-valued subtraction operator to
get all six relational operators.

But the relationships between composite/non-composite operators would yield
a huge benefit for many classes such as strings or matrices.  Consider the
expression "a = b + c + d" when all the variables are strings.  With most
string class implementations, the code generated for this is quite
inefficient.  Lots of needless temporary creation, copying, and destruction
is carried out.  If the programmer using the string class were familiar with
its implementation, and wanted the best performance, he would instead write:

	a = b; a += c; a += d;

But this is not as natural as the first expression, and it's unreasonable to
expect programmers to write like that.  Besides, replacing an expression
like "func(a + b + c + d)" is even harder.  Other string classes work around
this by inventing extra "helper" classes that are basically encoded
instructions for which strings to add together during an assignment.

The best solution is for the compiler to feel free to translate the first
expression into the second, taking advantage of implicit relationships
between operators, like C does.

I know this kind of thing will never be accepted as C++ standard, but it
should.	
---
John Scott Peter XXXIII I/III  // Wrong thinking is punishable
Programmer of Pixels          // Right thinking will be as quickly rewarded
Venice, CA                   // You will find it an effective combination



---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Implicit operation relationships
#2422
Author: Ulf Schuenemann
Date: Fri, 03 Nov 1995 19:21
46 lines
2295 bytes
In article <46vaga$dng@marina.cinenet.net>, scotty@cinenet.net (J Scott
Peter XXXIII I/III) writes:
|> a += b	=>  a = a + b
|> a + b	=>  tmp = a; a + b; tmp
|> Similarly for other composite operators.
|> 
|> a != b	=> !(a == b)	and vice versa
|> a >= b	=> !(a < b)	etc
|> a < b	=> a - b < 0	etc
|> a - b	=> a + (-b)	and vice versa
|> -b		=> 0 - b
|> +b		=> b
|> a++		=> a += 1	also --
|> a->b		=>  (*a).b

The standard header <utility> (20.2.1) already defines template-
operators for the relational operators, deducing them to == and <
a != b		=> !(a == b)
a >  b		=>   b < a
a <= b		=> !(b < a)
a >= b		=> !(a < b)

You can extend this idea:

template<class T> T& operator+=(T&a, T&b) { return a = a + b; }
template<class T> T  operator- (T&a, T&b) { return a + (-b); }
template<class T> T  operator- (T&a)      { return 0 - a; }
template<class T> T& operator+ (T&a)      { return a; }
template<class T> T& operator++(T&a)      { return a += 1; }
template<class T> T& operator++(T&a,int)  { return ++a; }

But unfortunately you can't do it this way for -> as it must be
a nonstatic memberfct (13.5.6 [over.ref]).


Ulf Schuenemann

--------------------------------------------------------------------
 ,_.   Ulf Schuenemann
#,  \  Fakultaet fuer Informatik, Technische Universitaet Muenchen, Germany.
 |   > email: schuenem@informatik.tu-muenchen.de
 v=-<  WWW:   http://hphalle2.informatik.tu-muenchen.de/~schuenem/
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Implicit operation relationships
#2443
Author: John Barton
Date: Tue, 07 Nov 1995 04:36
47 lines
2506 bytes
In article <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>,
schuenem@informatik.tu-muenchen.de (Ulf Schuenemann) writes:
|> In article <46vaga$dng@marina.cinenet.net>, scotty@cinenet.net (J Scott
|> Peter XXXIII I/III) writes:
|> |> a += b	=>  a = a + b
|> |> a + b	=>  tmp = a; a + b; tmp
|> |> Similarly for other composite operators.
|> |>
|> |> a != b	=> !(a == b)	and vice versa
|> |> a >= b	=> !(a < b)	etc
|> |> a < b	=> a - b < 0	etc
|> |> a - b	=> a + (-b)	and vice versa
|> |> -b		=> 0 - b
|> |> +b		=> b
|> |> a++		=> a += 1	also --
|> |> a->b		=>  (*a).b
|>
|> The standard header <utility> (20.2.1) already defines template-
|> operators for the relational operators, deducing them to == and <
|> a != b		=> !(a == b)
|> a >  b		=>   b < a
|> a <= b		=> !(b < a)
|> a >= b		=> !(a < b)
|>
|> You can extend this idea:
|>
|> template<class T> T& operator+=(T&a, T&b) { return a = a + b; }
|> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
|> template<class T> T  operator- (T&a)      { return 0 - a; }
|> template<class T> T& operator+ (T&a)      { return a; }
|> template<class T> T& operator++(T&a)      { return a += 1; }
|> template<class T> T& operator++(T&a,int)  { return ++a; }

This will not work: it prevent you from writing any templates
for these operators over types that do not have "1" or "0"
or that need a definition other than the one above.

--
John.

John J. Barton        jjb@watson.ibm.com            (914)784-6645
	<http://www.research.ibm.com/xw-SoftwareTechnology>
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Implicit operation relationships
#2454
Author: Robert Pearlman
Date: Tue, 07 Nov 1995 18:23
60 lines
2952 bytes
In article <47l76s$qu1@watnews2.watson.ibm.com>, jjb@watson.ibm.com (John Barton) writes:
|> In article <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>,
|> schuenem@informatik.tu-muenchen.de (Ulf Schuenemann) writes:
|> |> In article <46vaga$dng@marina.cinenet.net>, scotty@cinenet.net (J Scott
|> |> Peter XXXIII I/III) writes:
|> |> |> a += b	=>  a = a + b
|> |> |> a + b	=>  tmp = a; a + b; tmp
|> |> |> Similarly for other composite operators.
|> |> |>
|> |> |> a != b	=> !(a == b)	and vice versa
|> |> |> a >= b	=> !(a < b)	etc
|> |> |> a < b	=> a - b < 0	etc
|> |> |> a - b	=> a + (-b)	and vice versa
|> |> |> -b		=> 0 - b
|> |> |> +b		=> b
|> |> |> a++		=> a += 1	also --
|> |> |> a->b		=>  (*a).b
|> |>
|> |> The standard header <utility> (20.2.1) already defines template-
|> |> operators for the relational operators, deducing them to == and <
|> |> a != b		=> !(a == b)
|> |> a >  b		=>   b < a
|> |> a <= b		=> !(b < a)
|> |> a >= b		=> !(a < b)
|> |>
|> |> You can extend this idea:
|> |>
|> |> template<class T> T& operator+=(T&a, T&b) { return a = a + b; }
|> |> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
|> |> template<class T> T  operator- (T&a)      { return 0 - a; }
|> |> template<class T> T& operator+ (T&a)      { return a; }
|> |> template<class T> T& operator++(T&a)      { return a += 1; }
|> |> template<class T> T& operator++(T&a,int)  { return ++a; }
|> 
|> This will not work: it prevent you from writing any templates
|> for these operators over types that do not have "1" or "0"
|> or that need a definition other than the one above.
|> 
|> --
|> John.
|> 
|> John J. Barton        jjb@watson.ibm.com            (914)784-6645
|> 	<http://www.research.ibm.com/xw-SoftwareTechnology>
|> H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598
|> ---
|> [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
|>   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
|>   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]

The substitution for a+=b also won't work; if evaluation of a
has a side effect the side effect will happen twice.  

--
rp

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Implicit operation relationships
#2471
Author: John Max Skaller
Date: Wed, 08 Nov 1995 17:25
26 lines
1845 bytes
jjb@watson.ibm.com (John Barton) wrote:
>In article <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>,
>|> You can extend this idea:
>|>
>|> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
>|> template<class T> T  operator- (T&a)      { return 0 - a; }
>|> template<class T> T& operator+ (T&a)      { return a; }
>|> template<class T> T& operator++(T&a)      { return a += 1; }
>|> template<class T> T& operator++(T&a,int)  { return ++a; }
>
>This will not work: it prevent you from writing any templates
>for these operators over types that do not have "1" or "0"
>or that need a definition other than the one above.

How does it prevent it? [Quote WP ??]

   
-- 
John Max Skaller               voice: 61-2-566-2189
81 Glebe Point Rd              fax:   61-2-660-0850
GLEBE NSW 2037                 email: maxtal@suphys.physics.oz.au
AUSTRALIA                      email: skaller@maxtal.com.au
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Implicit operation relationships
#2506
Author: John Barton
Date: Fri, 10 Nov 1995 19:16
36 lines
2076 bytes
In article <47qnbu$f48@oznet03.ozemail.com.au>, John Max Skaller <maxtal@suphys.physics.su.oz.au> writes:
|> jjb@watson.ibm.com (John Barton) wrote:
|> >In article <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>,
|> >|> You can extend this idea:
|> >|>
|> >|> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
|> >|> template<class T> T  operator- (T&a)      { return 0 - a; }
|> >|> template<class T> T& operator+ (T&a)      { return a; }
|> >|> template<class T> T& operator++(T&a)      { return a += 1; }
|> >|> template<class T> T& operator++(T&a,int)  { return ++a; }
|> >
|> >This will not work: it prevent you from writing any templates
|> >for these operators over types that do not have "1" or "0"
|> >or that need a definition other than the one above.
|>
|> How does it prevent it? [Quote WP ??]

template<class T>
Matrix<T> operator-(Matrix<T>& rhs) { /* doesn't matter what I code here */ }

The global operator-(T&) will always win over any more
specific template like the one above.  Unless the standard has
changed here as well.

--
John.

John J. Barton        jjb@watson.ibm.com            (914)784-6645
	<http://www.research.ibm.com/xw-SoftwareTechnology>
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Implicit operation relationships
#2520
Author: David Vandevoorde
Date: Sat, 11 Nov 1995 06:03
40 lines
2670 bytes
>>>>> "JB" == John Barton <jjb@watson.ibm.com> writes:
JB> In article <47qnbu$f48@oznet03.ozemail.com.au>, John Max Skaller <maxtal@su
phys.physics.su.oz.au> writes:
JB> |> jjb@watson.ibm.com (John Barton) wrote:
JB> |> >In article <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>,
JB> |> >|> You can extend this idea:
JB> |> >|>
JB> |> >|> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
JB> |> >|> template<class T> T  operator- (T&a)      { return 0 - a; }
JB> |> >|> template<class T> T& operator+ (T&a)      { return a; }
JB> |> >|> template<class T> T& operator++(T&a)      { return a += 1; }
JB> |> >|> template<class T> T& operator++(T&a,int)  { return ++a; }
JB> |> >
JB> |> >This will not work: it prevent you from writing any templates
JB> |> >for these operators over types that do not have "1" or "0"
JB> |> >or that need a definition other than the one above.
JB> |>
JB> |> How does it prevent it? [Quote WP ??]

JB> template<class T>
JB> Matrix<T> operator-(Matrix<T>& rhs) { /* doesn't matter what I code here */
 }

JB> The global operator-(T&) will always win over any more
JB> specific template like the one above.  Unless the standard has
JB> changed here as well.

AFAIK, this has never been true. My understanding is that now the
``most specialized'' (if determinable) specialization will be
selected. Before that, I think you would have an ambiguity.

A template `t<U, V, ...>' would be ``more specialized'' than a template
`t<X, Y, ...>' if there exists some subsitution for `X', `Y', ... that
makes `t<X, Y>' identical to `t<U, V>' but not vice versa.

	Daveed
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Implicit operation relationships
#2523
Author: Fergus Henderson
Date: Sat, 11 Nov 1995 08:55
36 lines
2116 bytes

rp@iscp.bellcore.com (Robert Pearlman) writes:

>jjb@watson.ibm.com (John Barton) writes:
>|> schuenem@informatik.tu-muenchen.de (Ulf Schuenemann) writes:
>|> |> template<class T> T& operator+=(T&a, T&b) { return a = a + b; }
>|> |> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
>|> |> template<class T> T  operator- (T&a)      { return 0 - a; }
>|> |> template<class T> T& operator+ (T&a)      { return a; }
>|> |> template<class T> T& operator++(T&a)      { return a += 1; }
>|> |> template<class T> T& operator++(T&a,int)  { return ++a; }
>|> 
>|> This will not work: it prevent you from writing any templates
>|> for these operators over types that do not have "1" or "0"
>|> or that need a definition other than the one above.

I don't think that is correct now that partial specialization is
allowed - if you need a different definition, can't you just define a
(template) specialization?

>The substitution for a+=b also won't work; if evaluation of a
>has a side effect the side effect will happen twice.  

That's definitely not correct.  Templates are not just macro substitution.
The arguments are only evaluated once.

--
Fergus Henderson             	WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au              	PGP: finger fjh@128.250.37.3


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads