I have tried providing getters of class A for my non-member serialize() function` since accessing from members is private.
template
You can use good old-fashioned friends:
Live On Coliru
template
class A {
public:
A(const T &id) : m_id(id) {}
private:
template friend void boost::serialization::serialize(Ar&,A&,const unsigned);
T m_id;
};
namespace boost {
namespace serialization {
template
void serialize(Archive &ar, A &a, const unsigned int)
{
ar & BOOST_SERIALIZATION_NVP(a.m_id);
}
}
}
You can use the getRef() approach. This
make_nvp (because you can't use a.getRef() as an XML element nameSadly, having the reference getter break encapsulation in a horrific way. I'd personally prefer to have
m_idpublic in the first place, instead.
Live On Coliru
template
class A {
public:
A(const T &id) : m_id(id) {}
T& getRef() { return m_id; }
T const& getRef() const { return m_id; }
private:
T m_id;
};
namespace boost {
namespace serialization {
template
void serialize(Archive &ar, A &a, const unsigned int)
{
ar & boost::serialization::make_nvp("m_id", a.getRef());
}
}
}
You can use a 'pimpl' style struct. You can forward declare a struct inside A<>:
template
class A {
public:
struct access;
A(const T &id) : m_id(id) {}
private:
T m_id;
};
That's less intrusive than the getRef() approach which simply breaks encapsulation all the way. Now, you can hide the private access inside this class:
namespace boost {
namespace serialization {
template
void serialize(Archive &ar, A &a, const unsigned int version)
{
A::access::serialize(ar, a, version);
}
}
}
Of course you still need to implement it, but this can be done in a separate header and doesn't influence class A<> (or any of its specializations) at all:
template
struct A::access {
template
static void serialize(Archive &ar, A &a, const unsigned int) {
ar & BOOST_SERIALIZATION_NVP(a.m_id);
}
};
See it Live On Coliru as well