gpt4 book ai didi

c++ - 用 `using` 定义的类型的前向声明

转载 作者:搜寻专家 更新时间:2023-10-31 00:12:16 24 4
gpt4 key购买 nike

假设我有这样的情况:

//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')

我不能省略 #includeA.hpp 中因为boost::variant想要完整的类型。

如何转发申报Ausing 定义?

如果不可能,我现在想知道如何避免大量样板代码来解决我的问题。

最佳答案

I can't omit #include's in A.hpp because boost::variant wants complete types.

这不相关,因为您没有在此处实例化 boost::variant

继续并省略 #include

( live demo )

然后你的问题就烟消云散了:

How to forward declare A which is defined with using?

不要。再次使用 using,或者更好的是,将 using 语句 boost 到它自己的 header 中,然后可以将其包含在任何需要的地方。

关于c++ - 用 `using` 定义的类型的前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30807029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com