Friend declaration in nested classes requiring forward declaration

I am trying to write non-intrusive boost :: serialization routines for a nested class with a private member. Unfortunately, I cannot convince g ++ that the serialization procedure is a friend of the inner class. G ++ seems to require a direct declaration of the serialization procedure, which in turn will require a direct declaration of the nested class, which in turn cannot be executed in C ++. Am I missing something or is it just not possible? clang ++, by contrast, does not require a direct declaration and has no problems with the code below. The following code illustrates the problem:

#include <boost/archive/text_oarchive.hpp>

class Outer;
//class Outer::Inner;   // Not valid C++

namespace boost
{
    namespace serialization
    {
        template <class Archive>
        void serialize(Archive &ar, Outer& outer, const unsigned int version);
        //template <class Archive>
        //void serialize(Archive &ar, Outer::Inner& inner, const unsigned int version); // Cannot be done since forward declaration of nested class not possible.
    }
}

class Outer
{
    class Inner
    {
        int member_{42};

        template <class Archive>
        friend void boost::serialization::serialize(Archive &ar, Outer::Inner &inner, const unsigned int version);  // This does not work with gcc since the compiler seems to expect a forward declaration, which cannot be done (see above).
    };

    Inner inner_;

    template <class Archive>
    friend void boost::serialization::serialize(Archive &ar, Outer &outer, const unsigned int version);
    template <class Archive>
    friend void boost::serialization::serialize(Archive &ar, Inner &inner, const unsigned int version);
};

namespace boost
{
    namespace serialization
    {
        template <class Archive>
        void serialize(Archive &ar, Outer& outer, const unsigned int version)
        {
            ar & outer.inner_;
        }

        template <class Archive>
        void serialize(Archive &ar, Outer::Inner& inner, const unsigned int version)
        {
            ar & inner.member_;
        }
    }
}

int main()
{
    Outer outer;

    boost::archive::text_oarchive(std::cout) << outer;
}

-std=c++11 -lboost_serialization. g++ , member_ , . g++ ?

+4
1

[dcl.meaning]/1:

, , (, , inline- ([namespace.def])) ; [...].

, ( ) . , GCC , , . ( , , , , , .)

, , , , ( ).

+2

Source: https://habr.com/ru/post/1620409/


All Articles