I'm trying to make some persistence stuff and I have a struct like this:
struct EntityPersistence {
template <typename Archive>
void persist(Archive &ar, Entity &)
{
}
};
Then, in my class Entity I have something like this:
static const EntityPersistence entityPersistence;
PERSISTENCE_CUSTOM(Entity, entityPersistence)
This macro does something like this:
#define PERSISTENCE_CUSTOM(Base, customPersistence) \
SERIALIZE(Base, customPersistence)
Following the chain... (here is where the important thing comes)
#define SERIALIZE(Base, customPersistence)
template <class Archive>
void serialize(Archive& ar)
{
serialize_custom(ar);
}
template <class Archive, class Base, decltype(customPersistence) &persistence = customPersistence>
std::enable_if_t<std::is_base_of<cereal::InputArchive<Archive>, Archive>::value && has_deserialize<std::remove_const<decltype(customPersistence)>::type, Archive&, Base&>() == true, void>
serialize_custom(Archive &ar)
{
persistence.deserialize(ar, const_cast<Base&>(*this));
}
Some missing code to check which functions are implemented in the Persistance struct in order to branch execution code in compile time:
template<class> struct sfinae_true : std::true_type{};
template<class T, class A0, class A1>
static auto test_deserialize(int)
-> sfinae_true<decltype(std::declval<T>().deserialize(std::declval<A0>(), std::declval<A1>()))>;
template<class, class A0, class A1>
static auto test_deserialize(long) -> std::false_type;
template<class T, class A0, class A1>
static auto test_persist(int)
-> sfinae_true<decltype(std::declval<T>().persist(std::declval<A0>(), std::declval<A1>()))>;
template<class, class A0, class A1>
static auto test_persist(long) -> std::false_type;
template<class T, class Arg1, class Arg2>
struct has_deserialize : decltype(::detail::test_deserialize<T, Arg1, Arg2>(0)){};
template<class T, class Arg1, class Arg2>
struct has_persist : decltype(::detail::test_persist<T, Arg1, Arg2>(0)){};
The error in question:
In member function ‘std::enable_if_t<(std::is_base_of<cereal::InputArchive<Archive>, Archive>::value && (has_deserialize<EntityPersistence, Archive&, Entity&>() == true)), void> Entity::serialize_custom(Archive&)’:
error: ‘const struct EntityPersistence’ has no member named ‘deserialize’
persistence.deserialize(ar, const_cast<Base&>(*this)); \
^
deserialize
function doesn't exists in EntityPersistence but this serialize_custom
specialization shouldn't either if the enable_if_t
would have done its job.
I have tested has_deserialize
struct outside this code and it works perfectly. Could this has something to do with the non-type template parameter in serialize_custom functions? Maybe it's evaluated before the enable_if_t?
Thanks in advance
I finally solved this problem with an intermediary method (in case anyone is interested):
template <class Archive>
void serialize(Archive& ar)
{
serialize_custom_helper(ar);
}
template <class Archive, decltype(customPersistence)& persistence = customPersistence> \
void serialize_custom_helper(Archive& ar)
{
serialize_custom(ar, persistence);
}
template <class Archive, class Base, class P>
std::enable_if_t<std::is_base_of<cereal::InputArchive<Archive>, Archive>::value && has_deserialize2<P, Archive&, Base&>() == true, void>
serialize_custom(Archive &ar, P& persistence)
{
persistence.deserialize(ar, const_cast<Base&>(*this));
}
...