How to create friends of a class at compile time?

Tejas notrealemail at gmail.com
Thu Jul 15 17:43:10 UTC 2021


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:

```
template <class _Cp, bool = __has_storage_type<_Cp>::value>
class __bit_reference
{
     typedef typename _Cp::__storage_type    __storage_type;
     typedef typename _Cp::__storage_pointer __storage_pointer;

     __storage_pointer __seg_;
     __storage_type    __mask_;

//PART I'M CONCERNED WITH ***********************************






     friend typename _Cp::__self;

     friend class __bit_const_reference<_Cp>;
     friend class __bit_iterator<_Cp, false>;





//END OF PART I'M CONCERNED WITH ************************
public:
     _LIBCPP_INLINE_VISIBILITY
     __bit_reference(const __bit_reference&) = default;

     _LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT
         {return static_cast<bool>(*__seg_ & __mask_);}
     _LIBCPP_INLINE_VISIBILITY bool operator ~() const _NOEXCEPT
         {return !static_cast<bool>(*this);}

     _LIBCPP_INLINE_VISIBILITY
     __bit_reference& operator=(bool __x) _NOEXCEPT
     {
         if (__x)
             *__seg_ |= __mask_;
         else
             *__seg_ &= ~__mask_;
         return *this;
     }

     _LIBCPP_INLINE_VISIBILITY
     __bit_reference& operator=(const __bit_reference& __x) 
_NOEXCEPT
         {return operator=(static_cast<bool>(__x));}

     _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {*__seg_ ^= 
__mask_;}
     _LIBCPP_INLINE_VISIBILITY __bit_iterator<_Cp, false> 
operator&() const _NOEXCEPT
         {return __bit_iterator<_Cp, false>(__seg_, 
static_cast<unsigned>(__libcpp_ctz(__mask_)));}
private:
     _LIBCPP_INLINE_VISIBILITY
     __bit_reference(__storage_pointer __s, __storage_type __m) 
_NOEXCEPT
         : __seg_(__s), __mask_(__m) {}
};

```

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?


More information about the Digitalmars-d-learn mailing list