The answer is not clear c++
c++
Solution
Whoever wrote the original answer to the test is wrong.
- This example comes (exactly) from the C++ standard itself, section § 11/7, [class.access], and has been copied by the one who wrote the "test"
The example goes even further in the standard, with templates (I'll omit them here):
class A {
typedef int I; // private member
I f();
friend I g(I);
static I x;
};
A::I A::f() { return 0; }
A::I g(A::I p = A::x);
A::I g(A::I p) { return 0; }
A::I A::x = 0;
- Quoting the standard for the explanation:
Here, all the uses of `A::I` are well-formed because `A::f` and `A::x` are members of class A and g is a friend of class A. This implies, for example, that access checking on the first use of `A::I` must be deferred until it is determined that this use of `A::I` is as the return type of a member of class A. ]
- It compiles with both gcc and clang
Problem
Here is a test question: Consider the following code: ``` class A { typedef int I; // private member I f(); friend I g(I); static I x; }; ``` Which of the following are valid: ``` a. A::I A::f() { return 0; } b. A::I g(A::I p = A::x); c. A::I g(A::I p) { return 0; } d. A::I A::x = 0; ``` Answer to this question is considered correct only the first version (a.), but why? All them are valid in my opinion. Even tested all they compile successfully. Why only the first answer is correct?