I can do it like this in C++:
template<class abc>
class def
{
friend typename abc;
}
I am just hopelessly confused on how to achieve the same in D.
Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 15, 2021 How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
I can do it like this in C++:
I am just hopelessly confused on how to achieve the same in D. |
July 15, 2021 Re: How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tejas | On Thursday, 15 July 2021 at 17:21:45 UTC, Tejas wrote: >I can do it like this in C++: You don't just put class def and class abc in the same module and you get the same effect though. |
July 15, 2021 Re: How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tejas | On Thursday, 15 July 2021 at 17:21:45 UTC, Tejas wrote: >I can do it like this in C++:
I am just hopelessly confused on how to achieve the same in D. Uncharitably: D is a friendless language. Charitably: D is so much more friendly that instead of a short explicit list of friends, D has a large implicit list of friends: everything else in a module. That's why this works:
But this doesn't:
Error: no property |
July 15, 2021 Re: How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tejas | On Thu, Jul 15, 2021 at 05:21:45PM +0000, Tejas via Digitalmars-d-learn wrote: > I can do it like this in C++: > ``` > template<class abc> > class def > { > friend typename abc; > } > ``` > > I am just hopelessly confused on how to achieve the same in D. D does not have `friend` declarations. Equivalent functionality is achieved by `private` being module-private rather than aggregate-private, meaning that all you have to do is to put `abc` in the same module as `def`, and it will have access to all `def`s private members. If, however, you're trying to inject friend access to something outside def's module, then you might want to reconsider what you're trying to accomplish and whether it can be done differently. T -- Obviously, some things aren't very obvious. |
July 15, 2021 Re: How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D Ruppe | On Thursday, 15 July 2021 at 17:26:41 UTC, Adam D Ruppe wrote: >On Thursday, 15 July 2021 at 17:21:45 UTC, Tejas wrote: >I can do it like this in C++: You don't just put class def and class abc in the same module and you get the same effect though. I really should've just posted the whole thing... I know how modules and access specifiers work in D I was just reading the source of one of the files of libcxx and came across this:
How do you write the equivalent of that in D? Is the answer still the same? Manually keep it in the same module, or is there a programmatic way of converting this to D? |
July 15, 2021 Re: How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh |
>
> If, however, you're trying to inject friend access to something outside def's module, then you might want to reconsider what you're trying to accomplish and whether it can be done differently.
>
>
>
Was just dreaming of how to transpile C++ code to D :(
Curiously enough, friend injection was not made part of the standard back in 1998 and only g++ supported it through a non-standard extension. But now here we are. I wonder why this was allowed.
|
July 15, 2021 Re: How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to jfondren | On Thursday, 15 July 2021 at 17:30:05 UTC, jfondren wrote: >On Thursday, 15 July 2021 at 17:21:45 UTC, Tejas wrote: >I can do it like this in C++:
I am just hopelessly confused on how to achieve the same in D. Uncharitably: D is a friendless language. Charitably: D is so much more friendly that instead of a short explicit list of friends, D has a large implicit list of friends: everything else in a module. That's why this works:
But this doesn't:
Error: no property I'm sorry, I should've explicitly mentioned I'm interested in learning how to do friend injection in D. I know that access specifiers operate at module scope, seen a few posts about that here already. |
July 15, 2021 Re: How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tejas | On 7/15/21 1:43 PM, Tejas wrote: >How do you write the equivalent of that in D? Is the answer still the same? Manually keep it in the same module, or is there a programmatic way of converting this to D? Functions in the same module can access So the answer to your question depends on how the C++ code is organized. It's always possible that something in C++ here is not translatable into D, but most likely can be redesigned (and likely better designed). -Steve |
July 15, 2021 Re: How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tejas | On Thursday, 15 July 2021 at 17:49:06 UTC, Tejas wrote: >I'm sorry, I should've explicitly mentioned I'm interested in learning how to do friend injection in D. I know that access specifiers operate at module scope, seen a few posts about that here already. Probably the only way is CRTP (unlikely) or mixin that access internals. Both will be limited and cumbersome to use though. Additionally there is "package" visibility kind that takes optional package name to give access to specific package. https://dlang.org/spec/grammar.html#attributes (under visibility attributes) I rarely use package visibility so might be incorrect, but here an example anyway.
|
July 15, 2021 Re: How to create friends of a class at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to evilrat | On Thursday, 15 July 2021 at 18:11:30 UTC, evilrat wrote: >On Thursday, 15 July 2021 at 17:49:06 UTC, Tejas wrote: >I'm sorry, I should've explicitly mentioned I'm interested in learning how to do friend injection in D. I know that access specifiers operate at module scope, seen a few posts about that here already. Probably the only way is CRTP (unlikely) or mixin that access internals. Both will be limited and cumbersome to use though. Additionally there is "package" visibility kind that takes optional package name to give access to specific package. https://dlang.org/spec/grammar.html#attributes (under visibility attributes) I rarely use package visibility so might be incorrect, but here an example anyway.
CRTP and mixins are our best solution against multiple inheritance. Doubt they'll help with friend injection. I also could only think of package specifiers for access control, but wondered whether it could be drilled down to a specific module, rather than package level. you think Thanks for replying, much appreciated! |