私はヘッダーのみのライブラリを構築しており、コードが示すものと同様のことを行うことで、いくつかの循環依存の問題を解決しました。
基本的に、前方宣言された型が含まれていて前方宣言されていないかのように使用できるようにするプライベート テンプレート実装を作成します。
私のアプローチに危険なことはありますか?
パフォーマンスの低下はありますか? (ライブラリの主な焦点はパフォーマンスです - 実際のコードには明示的なinline
提案があります)
おまけの質問:コンパイル時間に (正または負の) 影響はありますか?
// Entity.h
#include "Component.h"
struct Entity { void a() { ... } }
// Component.h
struct Entity; // forward-declaration
class Component
{
Entity& entity;
template<class T = Entity> void doAImpl() { static_cast<T&>(entity).a(); }
public:
// void notWorking() { entity.a(); } <- this does not compile
void doA() { doAImpl(); }
}