0

I am using boost (de)serialization to reconstruct classes without public default ctors, by design. I declare a private default ctor and declare the boost access class as a friend, and it uses the default ctor as part of its deserialization process. But it can't reconstruct a std::pair unless I make the default ctor public. I'd rather declare friendship, but I can't figure out how.

Below is a simplified example:

class PrivateDC
{
private:
    friend class PrivateDCFriend;
    friend struct std::pair<int, PrivateDC>;
    PrivateDC() = default;
};
class PrivateDCFriend
{
    void f() const
    {
        PrivateDC a;                  // this works, proves friendship
        std::pair<int, PrivateDC> b;  // gets compile error
    }
};

The error is "no matching constructor for initialization of 'std::pair<int, PrivateDC>'". I don't get the error if the default ctor is public.
How can I declare std::pair as a friend so this will work?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user1414050
  • 99
  • 1
  • 2

1 Answers1

2

You can't,

Default construction of std::pair<T, U> requires that both T and U be DefaultConstructible and PrivateDC is not, failing at that requirement causes the default constructor of std::pair being excluded from the overload set.

Jans
  • 11,064
  • 3
  • 37
  • 45