簡體   English   中英

使用`using`定義的類型的前向聲明

[英]Forward declaration of type defined with `using`

假設我有這種情況:

//A.hpp
#include "B.hpp"
#include "C.hpp"
#include "D.hpp"

using A = boost::variant<B, C, D>;

//B.hpp
#include <memory>

class A;
using AA = std::unique_ptr<A>;

這給了我以下錯誤: error: typedef redefinition with different types ('boost::variant<B, C, D>' vs 'A')

我不能在A.hpp省略#include ,因為boost::variant完整的類型。

如何轉發聲明using定義的A

如果不可能,我現在想如何避免大量的樣板代碼來解決我的問題。

我不能在A.hpp省略#include ,因為boost::variant完整的類型。

這無關緊要,因為您沒有在此處實例化boost::variant

繼續並省略#include

現場演示

然后您的問題消失了:

如何轉發聲明使用定義的A?

別。 再次使用using ,或者更好的是,將using語句提升到其自己的標頭中,然后可以在需要的地方包含它。

只需替換class A; 在B.hpp中using A = boost::variant<B, C, D>;

using關鍵字不轉發任何聲明; 它只是聲明了類型別名。 因此,當在“ A.hpp”中包含“ B.hpp”時,將同一個轉換類(稱為A的類的向前聲明)和類型別名(稱為A)的聲明放入同一個轉換單元中。

暫無
暫無

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

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