41

I want to declare std::make_unique function as a friend of my class. The reason is that I want to declare my constructor protected and provide an alternative method of creating the object using unique_ptr. Here is a sample code:

#include <memory>

template <typename T>
class A
{
public:
    // Somehow I want to declare make_unique as a friend
    friend std::unique_ptr<A<T>> std::make_unique<A<T>>();


    static std::unique_ptr<A> CreateA(T x)
    {
        //return std::unique_ptr<A>(new A(x)); // works
        return std::make_unique<A>(x);         // doesn't work
    }

protected:
    A(T x) { (void)x; }
};

int main()
{
    std::unique_ptr<A<int>> a = A<int>::CreateA(5);
    (void)a;
    return 0;
}

Right now I get this error:

Start
In file included from prog.cc:1:
/usr/local/libcxx-head/include/c++/v1/memory:3152:32: error: calling a protected constructor of class 'A<int>'
return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
                           ^
prog.cc:13:21: note: in instantiation of function template specialization 'std::__1::make_unique<A<int>, int &>' requested here
    return std::make_unique<A>(x);     // doesn't work
                ^
prog.cc:22:41: note: in instantiation of member function 'A<int>::CreateA' requested here
std::unique_ptr<A<int>> a = A<int>::CreateA(5);
                                    ^
prog.cc:17:5: note: declared protected here
A(T x) { (void)x; }
^
1 error generated.
1
Finish

What is the correct way to declare std::make_unique as a friend of my class?

Praetorian
  • 106,671
  • 19
  • 240
  • 328
TheCrafter
  • 1,909
  • 2
  • 23
  • 44
  • try compiling with clang - it complains: `main.cpp:17:39: error: friends can only be classes or functions - friend `std::unique_ptr> std::make_unique>();` with the error location being the template instantiation. – marko Nov 24 '15 at 22:43
  • Interestingly, compiler with -std=c++14 and this one disappears, and the errors point towards @Praetorian's answer below. – marko Nov 24 '15 at 22:48
  • @marko The above error is with clang. I am on MSVC (vs 2013) now though. Doesn't work there either. – TheCrafter Nov 24 '15 at 22:52
  • Ok. my bad - different standards versions. Besides this trick being hard to pull off, I question this as a contract design - it seems you are conflating rather that separating concerns here. – marko Nov 24 '15 at 23:03
  • Well the main problem is I don't know who to trust. For example, based on Praetorian's answer, it works on clang but not on msvc :P – TheCrafter Nov 24 '15 at 23:06
  • 1
    I would agree with the advice given that it is implementation dependant and that you can't rely on it working. Probably time for a redesign :/ – marko Nov 24 '15 at 23:17
  • You're also pushing your luck if you expect MSVC to be complaint with C++14 - even in the most recent versions. They seem to have more or less got C+11 nailed these days. – marko Nov 24 '15 at 23:19
  • @marko The irony is that a couple of weeks ago I was arguing with a friend about msvc. I kept telling him stuff like "aaaw come on, msvc is fine, you'll see". I am pretty sure I owe him a beer now! :P – TheCrafter Nov 24 '15 at 23:23
  • @marko Regarding your first comment, `make_unique` was added in C++14, so if you're using `-std=c++11` the compiler has no idea what `make_unique` you're referring to in the friend declaration. – Praetorian Nov 25 '15 at 16:42

1 Answers1

31

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.

Praetorian
  • 106,671
  • 19
  • 240
  • 328