簡體   English   中英

有沒有辦法解決此模板循環依賴

[英]Is there a way to resolve this template circular dependency

是否有通用的方法來解決模板中的此類循環依賴關系,或者無法進行工作?

#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...>

在Coliru上實時觀看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM