Question

Suppose I have a structure containing an array of objects of another structure type, like this:

struct foo { int x, y; };
struct bar { std::array<foo, 4> foos; };

Is it possible to portably write an expression which will evaluate to a pointer-to-member for a given attribute of a given array element? Something like &bar::foos[2]::x?

The application is embind: I'd like to map a nested C++ type like this to a linear JavaScript tuple if possible. If this does not work out using pointer-to-member, then I could probably try using getter and setter methods instead, but I would still like to know whether there is a way to obtain such a pointer-to-member.

Was it helpful?

Solution

I don't think you can do anything like that. foos[2] is not a member of bar, only foos is. You cannot get a pointer-to-member from something which is not a member. Paragraph 5.3.1/4 of the C++11 Standard specifies:

A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses.

This means the grammar itself prevents you from writing anything that does not look like:

&class_name::member_name

where class_name could itself be a qualified name. No subscript operator or other fancy notation is allowed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top