Topic: Does the standard require friend operator== rather than member operator==?


Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/07/31
Raw View
On 28 Jul 1999 19:02:34 GMT, "Darin Adler" <darin@bentspoon.com> wrote:

Communication is hard.  I've got it now.

[ On C::iterator == C::const_iterator ]

:     1) Does the standard require the above code to work or is it undefined?

AFAIKT it is not mentioned.  The standard also does not require
C::const_iterator == C::iterator to be usable either.

: Question 2 was the one that strayed into specifics of implementation.
:
:     2) Would a friend function rather than a member function make it work?
:
: I am now 99% sure that the answer to question 2 is yes.

I'll go 100% (been wrong many times) if it is a non-template friend of
the const_iterator.  A template friend of const_iterator could also be
used to make both directions unusable.  ;-)

I agree that it should be usable in both directions.

: So my new question is: Is this the kind of thing that should be written up
: in a Defect Report?

Worst first.  If I am correct, the standard makes no mention of
comparing const_iterator to iterator.  If I understand defects, this
would not qualify since it is a change not a correction.

Next, where to put it?  It would seem to belong in the iterators (24)
but there is no mention of the names iterator and const_iterator there.
That leaves containers (23) and likely other places where the two
terms are defined like string (21) and maybe streams.  Could it be put
in introduction (17)?

These thoughts are posted in the hope that more knowledgable folks may
offer some suggestions.

John
---
[ 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: "Marco Manfredini" <marco@taris.de>
Date: 1999/07/26
Raw View
Darin Adler <darin@bentspoon.com> schrieb in im Newsbeitrag:
7mo0il$1kc3@enews1.newsguy.com...
>
> Consider this program:
>
>     #include <list>
>     int main()
>     {
>         std::list<int> l;
>         std::list<int>::iterator i(l.begin());
>         std::list<int>::const_iterator ci(l.begin());
>         if (i == ci) return 1; // fails to compiler on my compiler/library

Try this:
         if (ci == i) return 1; // works
>     }
>

operator == is defined as a member for iterator and const_iterator:
  bool iterator iterator::operator==(const iterator &o)
  bool const_iterator const_iterator::operator==(const const_iterator &o)

const_iterator has a constructor const_iterator::const_iterator(const
iterator &o), so
"if (ci == i)" means: "if (ci == const_iterator::const_iterator(i))"

There is however, with good reason, no iteratir::iterator(const
const_iterator &o), so if (i==cu) must fail.

Read the stl implementation file you include: <list> to see what happens
there.
---
[ 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: "Darin Adler" <darin@bentspoon.com>
Date: 1999/07/28
Raw View
John Potter <jpotter@falcon.lhup.edu> wrote:

> For now, take the advice to use (ci == i) and put some pressure on
> vendors to provide the non-member versions.

I did both of these things before I posted the original question. I only
brought it to the attention of one vendor, Metrowerks, but I didn't need to
apply any pressure -- Howard Hinnant, the Metrowerks library engineer agreed
right away that a friend would be an improvement over the member in this
case!

> See 17.3.1.2/3 which states that requirements are specified without
> the interface.  The example is ++x which may be a member or not.

This section is clear. It says that the standard will describe the
requirements and leave the implementation as unconstrained as possible.

But my main question is whether the standard requires (i == ci) to work. I
framed the question as "is friend required", but my underlying desire is to
know whether there's a *requirement* to support (i == ci). Question 1 from
my original post's summary was about the requirement, not the implementation
technique:

    1) Does the standard require the above code to work or is it undefined?

Question 2 was the one that strayed into specifics of implementation.

    2) Would a friend function rather than a member function make it work?

I am now 99% sure that the answer to question 2 is yes.

> In particular to the original question about iterators, they are
> implementation defined things and nothing is said about how the
> requirements are satisfied.

Neither of my original questions were about the standard constraining how
the requirements are satisfied. I'm trying to figure out if the standard
requires (i == ci) to work. And if the standard doesn't require that, then
I'd like to write it up as a Defect Report.

So my new question is: Is this the kind of thing that should be written up
in a Defect Report?

It does seem weak that the standard leaves this undefined; it's so easy to
write sensible code that will work fine with some compilers/libraries but is
not compliant with the standard.

I'm not sure that I know how to write a suitable Defect Report, though.

[ moderator's note: Refer to the FAQ for this newsgroup. -sdc ]

    -- Darin

PSL Thanks for your response, John!


[ 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: "Darin Adler" <darin@bentspoon.com>
Date: 1999/07/18
Raw View
Consider this program:

    #include <list>
    int main()
    {
        std::list<int> l;
        std::list<int>::iterator i(l.begin());
        std::list<int>::const_iterator ci(l.begin());
        if (i == ci) return 1; // fails to compiler on my compiler/library
    }

Is the list<>::iterator class required to have a friend function for
operator ==, or is it allowed to implement it with a member function?

The reason I ask is that I think the comparison might work if the ==
operators for list<>::iterator and list<>::const_iterator were separate
functions rather than member functions.

Summary:

    1) Does the standard require the above code to work or is it undefined?
    2) Would a friend function rather than a member function make it work?

    -- Darin


[ 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: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/07/19
Raw View
On 18 Jul 1999 16:31:35 GMT, Darin Adler <darin@bentspoon.com> wrote:

>    #include <list>
>    int main()
>    {
>        std::list<int> l;
>        std::list<int>::iterator i(l.begin());
>        std::list<int>::const_iterator ci(l.begin());
>        if (i == ci) return 1; // fails to compiler on my compiler/library
>    }
>
>Is the list<>::iterator class required to have a friend function for
>operator ==, or is it allowed to implement it with a member function?
>
>The reason I ask is that I think the comparison might work if the ==
>operators for list<>::iterator and list<>::const_iterator were separate
>functions rather than member functions.
>
>Summary:
>
>    1) Does the standard require the above code to work or is it undefined?
>    2) Would a friend function rather than a member function make it work?

1. The standard doesn't require the above to work.

2. If operator== is a friend function, and there is an implicit conversion
   function to convert iterator into const_iterator, then the above code
   works.  The standard doesn't say whether this conversion function should
   exist, and it also doesn't say whether operator== should be a member or
   a friend.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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              ]