非类型模板参数和std :: enable_if_t

时间:2019-02-08 16:53:29

标签: c++ templates sfinae enable-if

我正在尝试制作一些持久性东西,并且我有一个像这样的结构:

struct EntityPersistence {
    template <typename Archive>
    void persist(Archive &ar, Entity &)
    {
    }
};

然后,在我的实体班级中,我有类似这样的内容:

static const EntityPersistence entityPersistence;

PERSISTENCE_CUSTOM(Entity, entityPersistence)

此宏的功能如下:

#define PERSISTENCE_CUSTOM(Base, customPersistence)  \
SERIALIZE(Base, customPersistence)

遵循链条...(这是重要的地方)

#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));                                    
}

一些缺少的代码来检查在Persistance结构中实现了哪些功能,以便在编译时分支执行代码:

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)){};

有问题的错误

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函数在EntityPersistence中不存在,但是如果serialize_custom将完成其工作,则此enable_if_t专业化也不应该。 我已经在此代码之外测试了has_deserialize结构,它可以正常工作。这可能与serialize_custom函数中的非类型模板参数有关吗?也许要在enable_if_t之前进行评估?

预先感谢

2 个答案:

答案 0 :(得分:0)

不确定,我有足够的元素可以尝试,但是...检查persistenceserialize_custom()的模板参数)而不是customPersistence(不是模板参数)怎么办?的serialize_custom()中?

我的意思是……怎么办?

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(persistence)>::type,
                              Archive&, Base&>() == true> //^^^^^^^^^^^
serialize_custom(Archive &ar)
{
    persistence.deserialize(ar, const_cast<Base&>(*this));                                    
}

答案 1 :(得分:0)

我终于用一种中介方法解决了这个问题(如果有人感兴趣的话):

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));
}

...