I am reading "C++ Templates. The Complete Guide. Second Edition", by David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor. And, there is something that I don't understand.
This is one code example in pg. 59:
// define binary tree structure and traverse helpers:
struct Node {
int value;
Node* left;
Node* right;
Node(int i=0) : value(i), left(nullptr), right(nullptr) {}
// ...
};
auto left = &Node::left;
auto right = &Node::right;
// traverse tree, using fold expression:
template<typename T, typename... TP>
Node* traverse (T np, TP... paths) {
return (np ->* ... ->* paths);
}
// np ->* paths1 ->* paths2 ...
int main()
{
// init binary tree structure:
Node* root = new Node{0};
root->left = new Node{1};
root->left->right = new Node{2};
// ...
// traverse binary tree:
Node* node = traverse(root, left, right);
//...
}
What I don't understand is:
auto left = &Node::left;
auto right = &Node::right;
These are global variables of type (according to the llvm compiler): Node *Node::*
. With value, for example the first one, left
: &Node::left
The question is, being the variable left
a pointer, how can I take the address (operator&) of a non-instantiated struct?