banging my head on the c++, again
Sep. 2nd, 2008 08:02 pmIs it possible in C++ to declare and initialize a pointer-to-member that points to a data member of a data member? Concretely, given
struct A { int x; int y; };
struct B { A i; A ii; A iii; A iv; };
I would like to be able to do something like this:
TYPE piix = INITIALIZER<B, ii, x>; ... B b; int x_within_ii = b.*piix;
Is this possible? If so, exactly how do you write TYPE and INITIALIZER? If not, why the hell not?
no subject
Date: 2008-09-03 02:24 pm (UTC)int B::* piix = &B::ii.x;
B b;
int x_within_ii = b.*piix;
But ii is not allowed in INITIALIZER so you're out of luck. I'm guessing it would just have complicated the grammar too much; &B::ii is a qualified-id and those are supposed to refer to direct members of the qualifying scope. I don't know if it would parse as "&B::ii" . "x" or & "B::ii.x", for one thing.
no subject
Date: 2008-09-03 10:30 pm (UTC)GCC's error messages for this could be improved - "invalid use of non-static data member B::ii" leaves the possibility open that if you just wrote either the type or the initializer slightly differently it would be valid...