Date: Sat, 21 Jun 2025 15:34:50 +0000
> OK: *how* would it be useful? Why would you want a class to be a
> friend just because that class has a default constructor, or is a base
> class of something? Where would this stuff actually be useful?
Just a example.
Now imagine you are writing a program:
a class named A, it has a lot of data.
another class named Base, the A shares its data for Base and the derived classes from Base.
then you may have:
class A { /* ... */ };
class B
{
protected:
int& get_i() { return A::i; }
float& get_f() { return A::f; }
// ...
};
class C : public B
{
public:
int foo()
{
return get_i() + ... * get_f() - get_i() ...;
}
};
if we need add new data for A, we must add functions named get_data1, get_data2, ...
use friend with conditions:
class A { /* ... */ };
class B { /* ... */ };
class C : public B
{
public:
int foo()
{
return i + ... * f - i ...;
}
}
ADDITIONAL: IT JUST A EXAMPLE.
3 points:
1. Be easy to write friend for all derived classes.
2. Avoid too many interfaces.
3. No function calling, be easy to debug.
> Also, what is `T` in the above example?
Like template<typename T>.
> Was there supposed to be a template header somewhere?
You're right. The example above has conflicting syntax. But it doesn't matter, we can revise anytime.
template<typename T>
friend T if(...);
is OK but I don't think is the best syntax.
________________________________
发件人: Std-Discussion <std-discussion-bounces_at_lists.isocpp.org> 代表 Jason McKesson via Std-Discussion <std-discussion_at_[hidden]>
发送时间: 2025年6月21日 21:43
收件人: std-discussion_at_[hidden] <std-discussion_at_[hidden]>
抄送: Jason McKesson <jmckesson_at_[hidden]>
主题: Re: [std-discussion] A Idea: Friend with Conditions
On Sat, Jun 21, 2025 at 6:15 AM SD SH via Std-Discussion
<std-discussion_at_[hidden]pp.org> 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.
OK: *how* would it be useful? Why would you want a class to be a
friend just because that class has a default constructor, or is a base
class of something? Where would this stuff actually be useful?
Also, what is `T` in the above example? Was there supposed to be a
template header somewhere?
--
Std-Discussion mailing list
Std-Discussion_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
> friend just because that class has a default constructor, or is a base
> class of something? Where would this stuff actually be useful?
Just a example.
Now imagine you are writing a program:
a class named A, it has a lot of data.
another class named Base, the A shares its data for Base and the derived classes from Base.
then you may have:
class A { /* ... */ };
class B
{
protected:
int& get_i() { return A::i; }
float& get_f() { return A::f; }
// ...
};
class C : public B
{
public:
int foo()
{
return get_i() + ... * get_f() - get_i() ...;
}
};
if we need add new data for A, we must add functions named get_data1, get_data2, ...
use friend with conditions:
class A { /* ... */ };
class B { /* ... */ };
class C : public B
{
public:
int foo()
{
return i + ... * f - i ...;
}
}
ADDITIONAL: IT JUST A EXAMPLE.
3 points:
1. Be easy to write friend for all derived classes.
2. Avoid too many interfaces.
3. No function calling, be easy to debug.
> Also, what is `T` in the above example?
Like template<typename T>.
> Was there supposed to be a template header somewhere?
You're right. The example above has conflicting syntax. But it doesn't matter, we can revise anytime.
template<typename T>
friend T if(...);
is OK but I don't think is the best syntax.
________________________________
发件人: Std-Discussion <std-discussion-bounces_at_lists.isocpp.org> 代表 Jason McKesson via Std-Discussion <std-discussion_at_[hidden]>
发送时间: 2025年6月21日 21:43
收件人: std-discussion_at_[hidden] <std-discussion_at_[hidden]>
抄送: Jason McKesson <jmckesson_at_[hidden]>
主题: Re: [std-discussion] A Idea: Friend with Conditions
On Sat, Jun 21, 2025 at 6:15 AM SD SH via Std-Discussion
<std-discussion_at_[hidden]pp.org> 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.
OK: *how* would it be useful? Why would you want a class to be a
friend just because that class has a default constructor, or is a base
class of something? Where would this stuff actually be useful?
Also, what is `T` in the above example? Was there supposed to be a
template header somewhere?
--
Std-Discussion mailing list
Std-Discussion_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
Received on 2025-06-21 15:34:59