[英]C++ circular dependency on public aliases (typdef/using) defined inside a class/struct
有沒有一種方法可以打破如下所示的循環依賴關系,而無需在類外移動using/typedef
別名?
// header A.h
#ifdef A
#define A
#include "B.h"
class A {
using ID = uint32_t;
void some_func(B::ID id);
};
#endif A
// header B.h
#ifdef B
#define B
#include "A.h"
class B {
using ID = uint64_t;
void some_func(A::ID id);
};
#endif B
// main.cpp
#include "A.h"
int main() {...}
假設存在警衛#ifdef
,並且ID可以是更復雜的類型( struct
等)。
編輯:需要澄清:別名不一定相同(即,不是ID
)。
修改示例:
// header A.h
#ifdef A
#define A
#include "B.h"
class A {
using SomeAliasInA = uint32_t;
void some_func(B::SomeAliasInB id);
};
#endif A
// header B.h
#ifdef B
#define B
#include "A.h"
class B {
using SomeAliasInB = std::string;
void some_func(A::SomeAliasInA id);
};
#endif B
創建用於類型定義的外部文件:
template<typename T>
struct types{};
class A;
class B;
template<>
struct types<A>{
using alias = std::string;
};
template<>
struct types<B>{
using ID = bool;
using alias_2 = int;
using alias_3 = short;
};
然后可以像這樣使用它:
class A{
void foo(types<B>::ID id);
};
class B{
void foo(types<A>::alias id);
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.