Date: Tue, 13 Jun 2023 13:38:33 +0200
wt., 13 cze 2023 o 13:10 Adrian Hall via Std-Proposals
<std-proposals_at_[hidden]> napisaĆ(a):
>
> Hello all.
>
> Proposal:
> Allow casting of Class::*member.submember to Class::*member.
>
> Toy example:
> See in Godbolt: https://godbolt.org/z/jeYo91GqK
>
> struct Vec3
> {
> double x,y,z;
> };
>
> struct Transform
> {
> Vec3 Position;
> //Quaterion Rotation; //not used in this example
> };
>
> class Potato
> {
> Transform ObjectTransform;
> //imagine other members
>
> public:
>
> template <typename T, T Potato::* Member>
> T GetInfo()
> {
> return this->*Member;
> }
>
> };
>
>
> template<>
> Transform Potato::GetInfo<Transform, &Potato::ObjectTransform>();
>
> The explicit function instantiation at the end compiles today. I
> propose to also allow the following:
>
> template<>
> double Potato::GetInfo<double,
> &Potato::ObjectTransform::Position::x>();
>
> Transform.Position.x is a member of Potato, why not let us pass a
> member pointer to it?
>
>
> Do people want this:
>
> Yes!
>
> Searching this issue pulls up stack overflow pages where people are
> lamenting that this is a hole in the standard:
> https://stackoverflow.com/questions/11837293/c-pointer-to-member-of-a-member
>
> I have spoken to multiple people about this proposal, each of whom
> have thought of cases where it would be useful to them.
>
> I have worked on a project where this would have significantly
> reduced boilerplate, since it would have saved writing a bunch of
> submember access functions. I am now working on another project where
> this would potentially allow a simple way of hooking various objects
> together by saying "use this submember of the target object as a data
> source".
>
> Would it be hard to do?
> As far as I am aware there is nothing in the rules of C++
> compilation that would disallow this, since the offset of the submember
> Transform.position.x is known within Potato. If we can write
> this->member.submember in a function, clearly we know the location of
> submember relative to this.
>
One solution I used in current standard is variadic template that get
multiple members pointers:
```
auto get = Get<&Foo::bar, &Bar::baz, &Baz::x>{};
get(Foo{}) = 1; // set inner `&Baz::x`
```
And this could support arrays and member functions too.
And for future version, I thinking if we should simply not allow + operators on
member functions like:
```
Foo::*int p = &Foo::bar + &Bar::baz + &Baz::x;
```
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
<std-proposals_at_[hidden]> napisaĆ(a):
>
> Hello all.
>
> Proposal:
> Allow casting of Class::*member.submember to Class::*member.
>
> Toy example:
> See in Godbolt: https://godbolt.org/z/jeYo91GqK
>
> struct Vec3
> {
> double x,y,z;
> };
>
> struct Transform
> {
> Vec3 Position;
> //Quaterion Rotation; //not used in this example
> };
>
> class Potato
> {
> Transform ObjectTransform;
> //imagine other members
>
> public:
>
> template <typename T, T Potato::* Member>
> T GetInfo()
> {
> return this->*Member;
> }
>
> };
>
>
> template<>
> Transform Potato::GetInfo<Transform, &Potato::ObjectTransform>();
>
> The explicit function instantiation at the end compiles today. I
> propose to also allow the following:
>
> template<>
> double Potato::GetInfo<double,
> &Potato::ObjectTransform::Position::x>();
>
> Transform.Position.x is a member of Potato, why not let us pass a
> member pointer to it?
>
>
> Do people want this:
>
> Yes!
>
> Searching this issue pulls up stack overflow pages where people are
> lamenting that this is a hole in the standard:
> https://stackoverflow.com/questions/11837293/c-pointer-to-member-of-a-member
>
> I have spoken to multiple people about this proposal, each of whom
> have thought of cases where it would be useful to them.
>
> I have worked on a project where this would have significantly
> reduced boilerplate, since it would have saved writing a bunch of
> submember access functions. I am now working on another project where
> this would potentially allow a simple way of hooking various objects
> together by saying "use this submember of the target object as a data
> source".
>
> Would it be hard to do?
> As far as I am aware there is nothing in the rules of C++
> compilation that would disallow this, since the offset of the submember
> Transform.position.x is known within Potato. If we can write
> this->member.submember in a function, clearly we know the location of
> submember relative to this.
>
One solution I used in current standard is variadic template that get
multiple members pointers:
```
auto get = Get<&Foo::bar, &Bar::baz, &Baz::x>{};
get(Foo{}) = 1; // set inner `&Baz::x`
```
And this could support arrays and member functions too.
And for future version, I thinking if we should simply not allow + operators on
member functions like:
```
Foo::*int p = &Foo::bar + &Bar::baz + &Baz::x;
```
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2023-06-13 11:38:46