samedi 26 septembre 2020

std::make_unique invoking private ctro from friend class

I am seeing a behavior of std::make_unique while creating an object from friend class. Following is minimum reproducible example

#include<iostream>
#include<memory>

class A 
{
    friend class B;
    private: 
    A() = default;
};

class B 
{
    private: 
    std::unique_ptr<A> dPtr;
    public: 
    B() 
    {
        //dPtr = std::make_unique<A>(); // This is a compiler error
        dPtr = std::unique_ptr<A>(new A()); // This is working 
    }
};

gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)

What i understand that std::make_unique is a template function which is not actually fried with class A. How can i make std::make_unique a friend function of class A so that std::make_unique work in my example?

Aucun commentaire:

Enregistrer un commentaire