How to create friends of a class at compile time?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jul 15 17:32:18 UTC 2021


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.


More information about the Digitalmars-d-learn mailing list