Date: Sat, 21 Jun 2025 19:11:57 +0000
The main reason is that access outsite the class is full allowed or full refused, we need to access and write members in need and sometimes we have to exposed the class to users, then we don't have a modifier named "public for me and private for others". To be honest, who likes using friend?
As your said, too many friend tells design problems. That's right.
Since most of persons don't like this idea, it is no need to continue to discuss it.
----
Why is there this discussion?
I'm trying to implement some non-existent syntax features.
And one of the step is that:
A template class has a member, and only another class and its derived classes can access the member.
For some reasons, I couldn't derive from the template class, another way is simply put the member into public members, I didn't do it because of unsafe, so it stalled.
Do you have a perfect program?
获取Outlook for Android<https://aka.ms/AAb9ysg>
________________________________
发件人: Std-Discussion <std-discussion-bounces_at_[hidden]> 代表 Chris Ryan via Std-Discussion <std-discussion_at_[hidden]>
发送时间: 星期日, 六月 22, 2025 1:02:12 上午
收件人: std-discussion_at_[hidden] <std-discussion_at_[hidden]>
抄送: Chris Ryan <chrisr98008_at_[hidden]>
主题: Re: [std-discussion] A Idea: Friend with Conditions
Not in response to just this post but all trying to modify friends & access.
The idea is we should be making the language simpler, not more complex.
If you are trying to give partial access to part of your class, you have a design problem.
Too many divergent features in one class (failed cohesiveness). Break it up. Move it around.
In general, using 'friend' is a code smell for anti-enpasilization and a weak design, (you left the backdoor open)
IMHO, One of the few valid uses is for hidden friends (ADL operator overloading).
On Sat, Jun 21, 2025 at 3:15 AM SD SH via Std-Discussion <std-discussion_at_[hidden]<mailto:std-discussion_at_[hidden]>> wrote:
How about adding conditions for friend?
Like this (or other?):
class B { };
class E
{
int i;
E(int i) : i(i) { }
};
class A
{
static int i = 0;
friend typename T if(std::is_base_of<B, T>::value); // if a class derived from B, it is a friend class of A
friend E if(std::is_default_constructible<E>::value); // E is not a friend class of A because std::is_default_constructible<E>::value == false
};
class C : B
{
public:
int geti()
{
return A::i; // OK, because std::is_base_of<B, C>::value == true
}
};
class D
{
public:
int geti()
{
return A::i; // Error, because std::is_base_of<B, D>::value == false
}
};
A Probably Syntax:
friend <type-id> if/*constexpr*/(<constexpr-bool-expression>);
It may be a useful syntax feature.
E. S. Himers
2025/06
--
Std-Discussion mailing list
Std-Discussion_at_[hidden]<mailto:Std-Discussion_at_[hidden]>
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
As your said, too many friend tells design problems. That's right.
Since most of persons don't like this idea, it is no need to continue to discuss it.
----
Why is there this discussion?
I'm trying to implement some non-existent syntax features.
And one of the step is that:
A template class has a member, and only another class and its derived classes can access the member.
For some reasons, I couldn't derive from the template class, another way is simply put the member into public members, I didn't do it because of unsafe, so it stalled.
Do you have a perfect program?
获取Outlook for Android<https://aka.ms/AAb9ysg>
________________________________
发件人: Std-Discussion <std-discussion-bounces_at_[hidden]> 代表 Chris Ryan via Std-Discussion <std-discussion_at_[hidden]>
发送时间: 星期日, 六月 22, 2025 1:02:12 上午
收件人: std-discussion_at_[hidden] <std-discussion_at_[hidden]>
抄送: Chris Ryan <chrisr98008_at_[hidden]>
主题: Re: [std-discussion] A Idea: Friend with Conditions
Not in response to just this post but all trying to modify friends & access.
The idea is we should be making the language simpler, not more complex.
If you are trying to give partial access to part of your class, you have a design problem.
Too many divergent features in one class (failed cohesiveness). Break it up. Move it around.
In general, using 'friend' is a code smell for anti-enpasilization and a weak design, (you left the backdoor open)
IMHO, One of the few valid uses is for hidden friends (ADL operator overloading).
On Sat, Jun 21, 2025 at 3:15 AM SD SH via Std-Discussion <std-discussion_at_[hidden]<mailto:std-discussion_at_[hidden]>> wrote:
How about adding conditions for friend?
Like this (or other?):
class B { };
class E
{
int i;
E(int i) : i(i) { }
};
class A
{
static int i = 0;
friend typename T if(std::is_base_of<B, T>::value); // if a class derived from B, it is a friend class of A
friend E if(std::is_default_constructible<E>::value); // E is not a friend class of A because std::is_default_constructible<E>::value == false
};
class C : B
{
public:
int geti()
{
return A::i; // OK, because std::is_base_of<B, C>::value == true
}
};
class D
{
public:
int geti()
{
return A::i; // Error, because std::is_base_of<B, D>::value == false
}
};
A Probably Syntax:
friend <type-id> if/*constexpr*/(<constexpr-bool-expression>);
It may be a useful syntax feature.
E. S. Himers
2025/06
--
Std-Discussion mailing list
Std-Discussion_at_[hidden]<mailto:Std-Discussion_at_[hidden]>
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
Received on 2025-06-21 19:12:03