[英]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.