In other words does this code work?
#include <iostream>
template <class T>
class ImFriendWith {
int privValue;
public:
ImFriendWith(int v): privValue{v} {};
friend class T; // <-- THIS
};
struct Befriended {
ImFriendWith<Befriended> secret;
Befriended(int v): secret{v} {};
int getValue() { return secret.privValue; }
};
int main (int argc, char **argv)
{
Befriended b{42};
std::cout << "The secret is: " << b.getValue() << std::endl;
return 0;
}
I tried to compile it with G++, but it splits out only errors. Also I haven't found anything like my example at https://en.cppreference.com/
However...
If I remove the text "class" next to "friend", then it compiles and works like I wanted: it prints "The secret is: 42".
I want to know:
- why it works, and
- is it portable?