C ++ unique_ptr vs private class of private destructor class

I have a layout like this:

class LexedFile
    {
    friend class Lex;
//...
private:
    ~LexedFile();
    };

class Lex
    {
//...
private:
    std::map<std::string, std::unique_ptr<LexedFile> > Files;
    };

Lex is the sole creator of objects LexedFileand retains ownership of all objects LexedFilethat he creates on the map. Unfortunately, the compiler complains about this because of visibility rules from the map variable to the destructor LexedFile. I can fix this problem by making it ~LexedFile()publicly available, but, of course, the reason I made it private is to strengthen the decision that objects of this type belong only to objects Lex.

My question is: what are my portable options for being unique_ptrhappy and ~LexedFile()private? Portable, I think it should at least work with the latest g ++ and the latest Visual C ++.

, - :

friend class std::unique_ptr<LexedFile>;

( ), , , .

+3
1

std::unique_ptr . , :

class LexedFile
{
    friend class Lex;

//...
private:
    struct Deleter
    {
        void operator()(LexedFile *file) const
        {
            delete file;
        }
    };

    ~LexedFile();
};

class Lex
{
//...
private:
    std::map<std::string, std::unique_ptr<LexedFile, LexedFile::Deleter>> Files;
};
+4

All Articles