有没有办法解决这个模板循环依赖关系

Is there a way to resolve this template circular dependency

本文关键字:循环 依赖 关系 解决 有没有      更新时间:2023-10-16

是否有通用方法来解决模板中的这种类型的循环依赖关系,还是无法正常工作?

#include <tuple>
template<class... T>
struct A {
    std::tuple<T...> t;
};
template<class type_of_A>
struct D1 {
    type_of_A* p;
};
template<class type_of_A>
struct D2 {
    type_of_A* p;
};
using A_type = A<D1<???>, D2<???>>;  // <------
int main() { }

像往常一样,在混合中插入一个命名的间接寻址以打破无限递归:

template<class... T>
struct A {
    std::tuple<T...> t;
};
template<class type_of_A>
struct D1 {
    typename type_of_A::type* p; // Indirection
};
template<class type_of_A>
struct D2 {
    typename type_of_A::type* p; // Indirection
};
// Type factory while we're at it
template <template <class> class... Ds>
struct MakeA {
    using type = A<Ds<MakeA>...>; // Hey, that's me!
};
using A_type = typename MakeA<D1, D2>::type;

MakeA注入的类名的行为是一个奖励,但我们可以将其拼写为 MakeA<Ds...> .

在科里鲁现场观看