Topic: How to overload operator ->*?
Author: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Date: 1997/02/27 Raw View
James Kanze <james-albert.kanze@vx.cit.alcatel.fr> wrote:
>(although I strongly recommend against it) define:
>
> MyDouble operator->*( MyDouble , MyDouble ) ;
>
Wow, why didn't I think of that! I was looking for a binary operator
that would bind closer than * (multiplication). It looks like I can
write (I'm serious about this, so please be gentle if you flame me):
class PowDouble
{
double d_;
public:
PowDouble(double d) : d_(d) { }
operator double () const { return d_ }
};
double operator->* (double d1, PowDouble d2) { return pow(d1, d2); }
#define POW ->*
double a, b;
double result = a * b POW 2; // a * pow(b, 2)
// Expands to a * b ->* 2
// Evaluates as a * (b->*PowDouble(2))
// Evaluates to a * pow(b, 2)
Correct precidence, correct semantics, syntactic sugar, the works! The
POW operator is left-associative. I don't know if that is what is
expected, but even if it should be right-associative, this is closer
than anything else I have seen to a working exponentiation operator. (We
could use EXP, for expontentiation, instead of POW if we wanted to).
-------------------------------------------------------------
Pablo Halpern phalpern@truffle.ultranet.com
I am self-employed. Therefore, my opinions *do* represent
those of my employer.
---
[ 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: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/02/13 Raw View
Fergus Henderson writes:
> Valentin Bonnard <bonnardv@pratique.fr> writes:
>> James Kanze wrote:
>>>
>>> The reason the draft doesn't mention it explicitly is that there is
>>> nothing to say. Unlike operator->, operator->* is just a plain binary
>>> operator, with no particular restrictions.
>> Does it mean that it's not possible to use it to define a better
>> smart pointer:
> You can achieve the same effect using a template member function:
[snip]
> In fact, it doesn't have to be a member template; an ordinary function
> template should work too, I think:
> template <class T, class PtrToMem>
> T* operator ->* (really_smart<T>& smart_ptr, PtrToMem ptr_to_mem) {
> return *smart_ptr .* ptr_to_mem;
> }
The template should actually be:
template <class T, class MemT>
MemT operator ->* (really_smart<T>& smart_ptr, MemT T::*ptr_to_mem) {
return *smart_ptr .* ptr_to_mem;
}
(returning T* is not useful, after all :-)
Anyway, this will work for data members, but not for function members:
struct foo {
int bar;
void baz() { cout << 2 << endl; }
foo() : bar(1) {}
};
int foo::* pbar = &foo::bar;
void (foo::* pbaz)() = &foo::baz;
main() {
really_smart<foo> f;
cout << (*f).*pbar << endl; // this will print 1
(*f.*pbaz)(); // this will print 2
cout << f->*pbar << endl; // this will print 1
(f->*pbaz)(); // this won't compile
}
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ 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: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/02/11 Raw View
James Kanze wrote:
>
> Srinivas Vobilisetti <Srinivas.Vobilisetti@mci.com> writes:
>
> |> In am going thru chapter 13 Overloading of ANSI C++ Draft dated April
> |> 28, 1995 located at Stanford University. The draft mentioned about the
> |> operator->* but did not specify the restrictions on overloading it or
> |> the meaning of operator->* in the context of overloading it (I mean
> |> something similar to how operator-> is explained in the draft). Can
> |> someone help me (give an example) on how to overload operator->*? I
> |> would appreciate a reply to my email address
> |> Srinivas.Vobilisetti@mci.com
>
> The reason the draft doesn't mention it explicitly is that there is
> nothing to say. Unlike operator->, operator->* is just a plain binary
> operator, with no particular restrictions. For example, you can
> (although I strongly recommend against it) define:
>
> MyDouble operator->*( MyDouble , MyDouble ) ;
>
> as the power operator.
>
> Any reasonable overloading I can think of, however, would have a class
> (smart pointer) on the left, a selector of some sort on the right, and
> return a reference to some object (or a helper class). Something along
> the lines of:
>
> FieldAccessor operator->*( Record , FieldSelector ) ;
Does it mean that it's not possible to use it to define a better
smart pointer:
template <class T>
class really_smart {
public:
T& operator* ();
T* operator-> ();
operator T* (); // the one I like (yes, I know
// everyone in c.s.c and c.l.c.m
// will blame me for that)
T* operator->* (); // Why not ?
};
People usually prefer ptr->foo () to (*ptr).foo ().
Just to be consistant, ptr->*foo () could be prefered
over (*ptr).*foo ();
--
Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
[ 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: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/02/11 Raw View
Valentin Bonnard <bonnardv@pratique.fr> writes:
>James Kanze wrote:
>>
>> The reason the draft doesn't mention it explicitly is that there is
>> nothing to say. Unlike operator->, operator->* is just a plain binary
>> operator, with no particular restrictions.
[...]
>Does it mean that it's not possible to use it to define a better
>smart pointer:
>
>template <class T>
>class really_smart {
>public:
> T& operator* ();
> T* operator-> ();
> operator T* (); // the one I like (yes, I know
> // everyone in c.s.c and c.l.c.m
> // will blame me for that)
>
> T* operator->* (); // Why not ?
You can achieve the same effect using a template member function:
template <class T>
class really_smart {
public:
template <class PtrToMem>
T* operator ->* (PtrToMem ptr_to_mem) {
return **this .* ptr_to_mem;
}
...
}
In fact, it doesn't have to be a member template; an ordinary function
template should work too, I think:
template <class T, class PtrToMem>
T* operator ->* (really_smart<T>& smart_ptr, PtrToMem ptr_to_mem) {
return *smart_ptr .* ptr_to_mem;
}
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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
]
Author: Srinivas Vobilisetti <Srinivas.Vobilisetti@mci.com>
Date: 1997/02/04 Raw View
In am going thru chapter 13 Overloading of ANSI C++ Draft dated April
28, 1995 located at Stanford University. The draft mentioned about the
operator->* but did not specify the restrictions on overloading it or
the meaning of operator->* in the context of overloading it (I mean
something similar to how operator-> is explained in the draft). Can
someone help me (give an example) on how to overload operator->*? I
would appreciate a reply to my email address
Srinivas.Vobilisetti@mci.com
Thanks,
Srinivas
---
[ 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
]
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/02/06 Raw View
Srinivas Vobilisetti <Srinivas.Vobilisetti@mci.com> writes:
|> In am going thru chapter 13 Overloading of ANSI C++ Draft dated April
|> 28, 1995 located at Stanford University. The draft mentioned about the
|> operator->* but did not specify the restrictions on overloading it or
|> the meaning of operator->* in the context of overloading it (I mean
|> something similar to how operator-> is explained in the draft). Can
|> someone help me (give an example) on how to overload operator->*? I
|> would appreciate a reply to my email address
|> Srinivas.Vobilisetti@mci.com
The reason the draft doesn't mention it explicitly is that there is
nothing to say. Unlike operator->, operator->* is just a plain binary
operator, with no particular restrictions. For example, you can
(although I strongly recommend against it) define:
MyDouble operator->*( MyDouble , MyDouble ) ;
as the power operator.
Any reasonable overloading I can think of, however, would have a class
(smart pointer) on the left, a selector of some sort on the right, and
return a reference to some object (or a helper class). Something along
the lines of:
FieldAccessor operator->*( Record , FieldSelector ) ;
--
James Kanze home: kanze@gabi-soft.fr +33 (0)1 39 55 85 62
office: kanze@vx.cit.alcatel.fr +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
-- Conseils en informatique industrielle --
---
[ 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 ]