gpt4 book ai didi

c++ - 非类型模板参数和 std::enable_if_t

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:09 30 4
gpt4 key购买 nike

我正在尝试做一些持久性的东西,我有一个这样的结构:

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

一些缺失的代码用于检查持久性结构中实现了哪些功能,以便在编译时分支执行代码:

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 中不存在,但是如果 enable_if_t 完成它的工作,这个 serialize_custom 特化也不应该。我已经在这段代码之外测试了 has_deserialize 结构,它运行良好。这可能与 serialize_custom 函数中的非类型模板参数有关吗?也许它在 enable_if_t 之前被评估了?

提前致谢

最佳答案

不确定,我有足够的元素可以尝试,但是...如何检查 persistence(serialize_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));
}

关于c++ - 非类型模板参数和 std::enable_if_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54596990/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com