Calling a template member function: Help me understand a code snippet from another StackOverflow post

With respect to Can a pointer to members bypass the member access level? , I would like to understand a piece of code in this question.

Paste the code snippet here.

#include <iostream>

template<typename Tag, typename Tag::type M>
struct Rob { 
  friend typename Tag::type get(Tag) {
    return M;
  }
};

// use
struct A {
  A(int a):a(a) { }
private:
  int a;
};

// tag used to access A::a
struct A_f { 
  typedef int A::*type;
  friend type get(A_f);
};

template struct Rob<A_f, &A::a>;

int main() {
  A a(42);
  std::cout << "proof: " << a.*get(A_f()) << std::endl;
}

      

Among several questions, I single out one question here that can hack the rest.

I don't understand the following expression in a function main

:

a.*get(A_f())

      

I understand (I think) what is get(A_F())

returning a pointer to a.a

. However, I don't understand the function get()

. In what framework is it actually defined? How is he addressed in this structure?

I have two additional questions that can be answered if this question has been answered, but I put them here. First, in the definition, the Rob

keyword is friend

used before the function get

that is being defined and also declared. I thought the keyword friend

could only be used before a function declaration to indicate that a function defined elsewhere has access to the private / protected members of the class. Can someone please explain?

Second, I don't understand the line template struct Rob<A_f, &A::a>;

. I don't understand the use of the template

sans keyword <...>

and I don't understand why there is no template definition - it seems to be some kind of formal declaration. Can someone please explain? Thank.

+3


source to share


1 answer


However, I don't understand the function get()

. In what framework is it actually defined? How is he addressed in this structure?

First of all: the function get()

is a friend of the class template A

and Rob

. He is not a member of any class. It's more of a free feature.

Now where exactly is this free function defined? Well, it is defined in the class template Rob

and at the same time it is declared as a friend

class template Rob

.

I thought the friend keyword could only be used before a function declaration to indicate that a function defined elsewhere has access to the private / protected members of the class. Can someone please explain?



Yes, it can be declared by a friend

class without defining it inside the class. It can also be defined within a class and at the same time can be declared by friend

that class. In your case, this is actually defined in the class itself.

Second, I don't understand the line template struct Rob<A_f, &A::a>;

.

This is called an explicit instance of the class template with the given tempate arguments. You can see many other syntaxes to explicitly create a template here (well, if it's a function template):

+2


source







All Articles