How to make std::make_unique a friend of my class
make_unique
perfect forwards the arguments you pass to it; in your example you're passing an lvalue (x
) to the function, so it'll deduce the argument type as int&
. Your friend
function declaration needs to be
friend std::unique_ptr<A> std::make_unique<A>(T&);
Similarly, if you were to move(x)
within CreateA
, the friend
declaration would need to be
friend std::unique_ptr<A> std::make_unique<A>(T&&);
This will get the code to compile, but is in no way a guarantee that it'll compile on another implementation because for all you know, make_unique
forwards its arguments to another internal helper function that actually instantiates your class, in which case the helper would need to be a friend
.