friends with phobos, workaround?

Idan Arye via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 9 16:44:13 PDT 2015


On Wednesday, 9 September 2015 at 20:19:44 UTC, Daniel N wrote:
> For the record, I think D made the right decision... omitting 
> friends.
>
> However there's one case in particular which I find useful, 
> anyone see a good workaround for this?
>
> #include <memory>
>
> class Friendly
> {
> private:
>   int val;
>   Friendly(int&& val) : val(val) {}
>   friend std::unique_ptr<Friendly> 
> std::make_unique<Friendly>(int&& val);
> };
>
> int main()
> {
>   auto yay = std::make_unique<Friendly>(1);
>   auto nay = new Friendly(1);
> }

How about using a mixin 
template(http://dlang.org/template-mixin.html)?




module makeunique;

mixin template MakeUnique(Args...)
{
     import std.typecons : Unique;

     static Unique!(typeof(this)) makeUnique(Args args)
     {
         return Unique!Friendly(new typeof(this)(args));
     }
}




module friendly;

import makeunique;

struct Friendly
{
private:
     int val;
     this(int val)
     {
         this.val = val;
     }

public:
     mixin MakeUnique!(int);
};





module app;

import std.stdio;
import std.typecons;

import friendly;

void main()
{
     auto yay = Friendly.makeUnique(1);
}



More information about the Digitalmars-d-learn mailing list