Topic: Pointer to member ariphmetic
Author: vyacheslav@NOkononenkoSPAM.net (Vyacheslav Kononenko)
Date: Mon, 6 Dec 2004 17:30:22 GMT Raw View
Richard Smith wrote:
> Vyacheslav Kononenko wrote:
>
>>template < class T, int offset > class property;
>>
>>class Foo {
>> int i;
>>public:
>> property< Foo, offsetof( Foo, pi ) - offsetof( Foo, i ) > pi;
>>};
>
>
> Is the property template supposed to be a way getting something like
> C++/CLI's properties? If so, I don't see why you need the variable i
> at all. Why can't you just do something like the following:
>
> template <typename T> class property {
> public:
> property( T const& val = T() ) : val(val) {}
> T const& operator()() const { return val; } // get
> void operator()( T const& new_val ) { val = new_val; } // set
>
> private:
> T val;
> };
>
> (Using whatever syntax for getting and setting you want.) Or, if you
> really need to access an externally-stored member, then:
>
> template <class Class, typename T, T Class::*MemPtr>
> class property {
> public:
> property( Class& obj ) : obj(obj) {}
> T const& operator()() const { return obj.*mp; }
> void operator()( T const& new_val ) { obj.*mp = new_val; }
>
> private:
> Class& obj;
> MemPtr mp;
> };
>
> Standards committee paper N1615 has further examples along these
> lines.
>
> --
> Richard Smith
>
> ---
> [ 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://www.jamesd.demon.co.uk/csc/faq.html ]
>
Good point. But what I am trying to do is to avoid keeping host
pointer/reference in property. If I can get offset of property inside
host I can calculate host address by property's "this" pointer later. It
maybe not a big deal to have one extra reference/pointer but it also
makes some problems. Non existance of copy ctor in your code shows that
for example.
--
Regards,
Slava
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: vyacheslav@NOkononenkoSPAM.net (Vyacheslav Kononenko)
Date: Tue, 30 Nov 2004 03:46:16 GMT Raw View
I think such thing as pointer to member arithmetic would be rather
usefull. For example:
struct Foo {
int i;
double d;
};
Foo *foo;
Then somthing like this:
size_t i_offset = &Foo::i - foo;
size_t d_offset = &Foo::d - foo;
Maybe even
size_t diff = &Foo::i - &Foo::d;
What do you think?
--
Regards,
Slava
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ron@sensor.com (Ron Natalie)
Date: Tue, 30 Nov 2004 16:56:40 GMT Raw View
Vyacheslav Kononenko wrote:
> size_t i_offset = &Foo::i - foo;
size_t i_offset = offsetof(Foo, i);
> size_t d_offset = &Foo::d - foo;
size_t d_offset = offsetof(Foo, d);
>
> Maybe even
> size_t diff = &Foo::i - &Foo::d;
>
size_t diff = offsetof(Foo, d) - offsetof(Foo, i);
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: vyacheslav@NOkononenkoSPAM.net (Vyacheslav Kononenko)
Date: Tue, 30 Nov 2004 17:13:24 GMT Raw View
Ron Natalie wrote:
> Vyacheslav Kononenko wrote:
>
>> size_t i_offset = &Foo::i - foo;
>
>
> size_t i_offset = offsetof(Foo, i);
>
>> size_t d_offset = &Foo::d - foo;
>
>
> size_t d_offset = offsetof(Foo, d);
>
>>
>> Maybe even
>> size_t diff = &Foo::i - &Foo::d;
>>
>
> size_t diff = offsetof(Foo, d) - offsetof(Foo, i);
>
> ---
> [ 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://www.jamesd.demon.co.uk/csc/faq.html ]
>
Hmm
Is it standard for C++ and is there guarantee that it will be evaluated
as constant expression?
--
Regards,
Slava
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ron@sensor.com (Ron Natalie)
Date: Tue, 30 Nov 2004 17:45:45 GMT Raw View
> Hmm
> Is it standard for C++ and is there guarantee that it will be evaluated
> as constant expression?
>
Yes, it is standard for C and C++.
18.1 of the C++ standard describes it as being in cstddef and refers
you to the C standard after warning you it only works on pod-structs and
pod-unions.
The C standard requires it to evaluate to a constant expression of
type size_t.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: house@usq.edu.au (Ron House)
Date: Tue, 30 Nov 2004 18:25:54 GMT Raw View
Vyacheslav Kononenko wrote:
> I think such thing as pointer to member arithmetic would be rather
> usefull.
>
> What do you think?
If you think it would be rather useful, you must have an application in
mind where you are permitted to do something useful. The example you
showed simply computed a value I (at least) don't want to know, nor do
you say why that value would be worth knowing. I suspect you just want
to get up to low-level highjinks. The language should move towards the
high level, not the low. Such features would be a hindrance.
--
Ron House house@usq.edu.au
http://www.sci.usq.edu.au/staff/house
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: vyacheslav@NOkononenkoSPAM.net (Vyacheslav Kononenko)
Date: Tue, 30 Nov 2004 21:14:28 GMT Raw View
Ron House wrote:
> Vyacheslav Kononenko wrote:
>
>> I think such thing as pointer to member arithmetic would be rather
>> usefull.
>> What do you think?
>
>
> If you think it would be rather useful, you must have an application in
> mind where you are permitted to do something useful. The example you
> showed simply computed a value I (at least) don't want to know, nor do
> you say why that value would be worth knowing. I suspect you just want
> to get up to low-level highjinks. The language should move towards the
> high level, not the low. Such features would be a hindrance.
>
I do not know if this is a low-level highjink but I was thinking about
properties:
class Foo {
int i;
public:
property< Foo::i, OFFSET > pi;
};
the problem is I do not know a syntax construction to pass pi offset of
&Foo::pi and &Foo::i (so I could use it to calculate address of i from
pi's "this" in setters/getters) Sorry if I made my previous post unclear.
--
Regards,
Slava
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ron@sensor.com (Ron Natalie)
Date: Wed, 1 Dec 2004 00:08:51 GMT Raw View
Vyacheslav Kononenko wrote:
> the problem is I do not know a syntax construction to pass pi offset of
> &Foo::pi and &Foo::i (so I could use it to calculate address of i from
> pi's "this" in setters/getters) Sorry if I made my previous post unclear.
What is "property" and "OFFSET" in your example?
You need to be explicit.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: vyacheslav@NOkononenkoSPAM.net (Vyacheslav Kononenko)
Date: Wed, 1 Dec 2004 20:21:50 GMT Raw View
Ron Natalie wrote:
> Vyacheslav Kononenko wrote:
>
>> the problem is I do not know a syntax construction to pass pi offset
>> of &Foo::pi and &Foo::i (so I could use it to calculate address of i
>> from pi's "this" in setters/getters) Sorry if I made my previous post
>> unclear.
>
>
> What is "property" and "OFFSET" in your example?
> You need to be explicit.
>
> ---
> [ 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://www.jamesd.demon.co.uk/csc/faq.html ]
>
template < class T, int offset > class property;
class Foo {
int i;
public:
property< Foo, offsetof( Foo, pi ) - offsetof( Foo, i ) > pi;
};
or even better:
class Foo {
int i;
public:
property< Foo, offsetof( Foo, pi ) > pi;
};
--
Regards,
Slava
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: richard@ex-parrot.com (Richard Smith)
Date: Sun, 5 Dec 2004 00:47:18 GMT Raw View
Vyacheslav Kononenko wrote:
>
> template < class T, int offset > class property;
>
> class Foo {
> int i;
> public:
> property< Foo, offsetof( Foo, pi ) - offsetof( Foo, i ) > pi;
> };
Is the property template supposed to be a way getting something like
C++/CLI's properties? If so, I don't see why you need the variable i
at all. Why can't you just do something like the following:
template <typename T> class property {
public:
property( T const& val = T() ) : val(val) {}
T const& operator()() const { return val; } // get
void operator()( T const& new_val ) { val = new_val; } // set
private:
T val;
};
(Using whatever syntax for getting and setting you want.) Or, if you
really need to access an externally-stored member, then:
template <class Class, typename T, T Class::*MemPtr>
class property {
public:
property( Class& obj ) : obj(obj) {}
T const& operator()() const { return obj.*mp; }
void operator()( T const& new_val ) { obj.*mp = new_val; }
private:
Class& obj;
MemPtr mp;
};
Standards committee paper N1615 has further examples along these
lines.
--
Richard Smith
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]