Topic: std::rel_ops (was: Why's of C++ -- Part 5 (implied member functions discussio)n)
Author: Chris Double <chris@double.co.nz>
Date: 1999/08/28 Raw View
postmast.root.admi.gov@iname.com (blargg) writes:
> You have the idea. The client has to add this extra crap when using Mine.
> I'd rather just be able to include the interface for Mine and be done with
> it. But how does one use std::rel_ops to do that, without introducing
> std::rel_ops in its interface file (that wouldn't be nice)? No way that I
> can see. This was the trick question.
Putting your class in its own namespace should do the trick:
namespace A
{
using std::rel_ops::operator>;
// etc for other rel_ops that you want your class to have
class Mine
{
// operator == and operator< defined here
};
}
void doit()
{
A::Mine a, b;
// Compiles fine - picks up the rel_ops operator>
a > b;
}
Now the client does not need to know about rel_ops, and it's not
globally injecting the template rel_ops into the global scope.
Chris.
---
[ 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/08/28 Raw View
blargg wrote:
> In article <37C44E21.21B31228@tribble.com>, David R Tribble
> <david@tribble.com> wrote:
> > #include <utility>
> > using namespace std::rel_ops;
> You have the idea. The client has to add this extra crap when using Mine.
Congratulation !
You have discovered that relops as currently
specified is useless.
--
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: postmast.root.admi.gov@iname.com (blargg)
Date: 1999/08/30 Raw View
In article <wkg1142g5n.fsf@double.co.nz>, Chris Double
<chris@double.co.nz> wrote:
> postmast.root.admi.gov@iname.com (blargg) writes:
>
> > You have the idea. The client has to add this extra crap when using Mine.
> > I'd rather just be able to include the interface for Mine and be done with
> > it. But how does one use std::rel_ops to do that, without introducing
> > std::rel_ops in its interface file (that wouldn't be nice)? No way that I
> > can see. This was the trick question.
>
> Putting your class in its own namespace should do the trick:
>
> namespace A
> {
> using std::rel_ops::operator>;
> // etc for other rel_ops that you want your class to have
Right. This is quite tedious.
using std::rel_ops::operator !=;
using std::rel_ops::operator >;
using std::rel_ops::operator >=;
using std::rel_ops::operator <=;
Did I forget one? Why should something be able to be specified
inconsistently? I should be able to say "I want the other relation
operators defined based on my definitions of == and <" without being able
to accidentally say "I want *some* of the other relation operators ..."
> class Mine
> {
> // operator == and operator< defined here
> };
> }
>
> void doit()
> {
> A::Mine a, b;
>
> // Compiles fine - picks up the rel_ops operator>
> a > b;
> }
>
> Now the client does not need to know about rel_ops, and it's not
> globally injecting the template rel_ops into the global scope.
>
---
[ 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: postmast.root.admi.gov@iname.com (blargg)
Date: 1999/08/28 Raw View
In article <37C44E21.21B31228@tribble.com>, David R Tribble
<david@tribble.com> wrote:
> blargg wrote:
> >
> > In article <37C1D871.3C81E23D@tribble.com>, David R Tribble
> > <david@tribble.com> wrote:
> >
[snip]
> > > How about:
> > >
> > > #include <utility>
> > > using namespace std::rel_ops;
> > >
> > > See 20.2 in the standard.
> >
> > What about it? Please show how it would work (yes, this is a trick
> > question - but try anyway).
>
> I'm not expert, but here goes:
>
> class Mine
> {
> ...
> public:
> bool operator <(const Mine &r) const;
> bool operator ==(const Mine &r) const;
> };
>
> #include <utility>
> using namespace std::rel_ops;
>
> void foo()
> {
> Mine a;
> Mine b;
>
> if (a < b) ...; // Calls Mine::operator<()
>
> if (a == b) ...; // Calls Mine::operator==()
>
> if (a <= b) ...; // Uses std::rel_ops::operator<=(),
> // which calls Mine::operator<()
>
> if (a != b) ...; // Uses std::rel_ops::operator!=(),
> // which calls Mine::operator==()
> }
>
> Am I missing something? Is it more complicated to use than that?
You have the idea. The client has to add this extra crap when using Mine.
I'd rather just be able to include the interface for Mine and be done with
it. But how does one use std::rel_ops to do that, without introducing
std::rel_ops in its interface file (that wouldn't be nice)? No way that I
can see. This was the trick question.
---
[ 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 ]