Talk:cpp/language/friend
From cppreference.com
I see you have operators, an example of conversions may be helpful too? From discussion @ stackoverflow http://stackoverflow.com/questions/41141119/c-friend-conversion-function
class t_conversion_helpers
{
public:
operator char ( ) ;
};
class t_friend_conversion_example
{
public:
friend t_conversion_helpers::operator char ( ) ;
};
86.140.194.68 01:28, 15 December 2016 (PST)Michael Collier
- following up on Talk:cpp/language/cast_operator --Cubbi (talk) 06:35, 15 December 2016 (PST)
friend function-definition (2) not accurate
The description of the second case is not accurate. For example, this code does not compile:
namespace foo {
class X {
int a;
friend void friend_set(X& p, int i) {
p.a = i; // this is a non-member function
}
public:
void member_set(int i) {
a = i; // this is a member function
}
};
}
int main()
{
foo::X a;
foo::friend_set(a, 0);
}
For an explanation, see https://stackoverflow.com/a/5695855.
78.45.209.17 12:21, 30 August 2019 (PDT)
- this is already noted on this page, last paragraph under Notes (and in more detail on the page that links to) --Cubbi (talk) 13:56, 30 August 2019 (PDT)
- I'd say this note is not sufficient. The current wording in (2): "Defines a non-member function, and makes it a friend of this class at the same time. Such non-member function is always inline." is certainly a very poor description of the actual behaviour, notes should be used to extend the primary description and not to fundamentally change its meaning. -- 78.45.209.17 14:16, 30 August 2019 (PDT)
namespace a {
class X {
friend void foo(X& p)
{
// anything...
}
};
}
- is equivalent to this code:
namespace a {
class X {
friend void foo(X& p);
};
void foo(X& p)
{
// anything...
}
}
- which it clearly isn't. -- 78.45.209.17 23:29, 30 August 2019 (PDT)
- The item non-member function is clear enough in my opinion. --Fruderica (talk) 16:52, 15 September 2019 (PDT)
- It is not clear enough because it does not say where the non-member function is defined, nor that it may not be visible for lookup. -- 78.45.209.17 06:20, 21 September 2019 (PDT)
- Such description is in cpp/language/friend#Notes. And the latter form you mentioned definitely defines a member function which is never a non-member function. --Fruderica (talk) 09:10, 21 September 2019 (PDT)
- It is not clear enough because it does not say where the non-member function is defined, nor that it may not be visible for lookup. -- 78.45.209.17 06:20, 21 September 2019 (PDT)
- The item non-member function is clear enough in my opinion. --Fruderica (talk) 16:52, 15 September 2019 (PDT)
- which it clearly isn't. -- 78.45.209.17 23:29, 30 August 2019 (PDT)
Error in Template friends example?
This
[...]
template<class T>
friend int* A<T*>::h(); // all A<T*>::h are friends:
// A<float*>::h(), A<int*>::h(), etc
[...]
does not compile for me (GCC 10.2.1)
I get the error: member 'int* A<T*>::h()' declared as friend before type 'A<T*>' defined
172.70.250.116 13:59, 2 April 2023 (PDT)
- The example actually comes from the standard, so it's most likely a compiler bug --Ybab321 (talk) 16:29, 2 April 2023 (PDT)
- BTW, the example runs with e.g. "clang++ -std=c++98 -Wall -Wextra -Wpedantic ..." with warnings:
warning: dependent nested name specifier 'A<T>::' for friend class declaration is not supported; turning off access control for 'X' [-Wunsupported-friend]
- C++98 is irrelevant here, it compiles with C++ 03, 11, ... 23...
- Adding the flag "-Wno-unsupported-friend" suppresses the warning.--Space Mission (talk) 03:42, 3 April 2023 (PDT)