Date: Tue, 13 Jun 2023 07:10:45 -0400
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.
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.
Received on 2023-06-13 11:10:52