C++ 使用 unique_ptr 进行前向声明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13414652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Forward declaration with unique_ptr?
提问by Zyx 2000
I have found it useful to use forward declaration of classes in combination with std::unique_ptr
as in the code below. It compiles and works with GCC, but the whole thing seem kind of strange, and I wonder if this is standard behaviour (i.e. required by the standard)? Since B isn't a complete type when I declare the unique_ptr
.
我发现将类的前向声明与std::unique_ptr
下面的代码结合使用很有用。它编译并与 GCC 一起工作,但整个事情看起来有点奇怪,我想知道这是否是标准行为(即标准要求)?由于 B 在我声明unique_ptr
.
A.hpp
hpp
#include <memory>
class B;
class A {
std::unique_ptr<B> myptr;
// B::~B() can't be seen from here
public:
~A();
};
A.cpp
A.cpp
#include "B.hpp"
//B.hpp has to be included, otherwise it doesn't work.
A::~A() = default; // without this line, it won't compile
// however, any destructor definiton will do.
I suspect this has to do with the destructor (and therefore the need to call the destructor of unique_ptr<B>
) is defined in a specific compilation unit (A.cpp).
我怀疑这与unique_ptr<B>
在特定编译单元 (A.cpp) 中定义的析构函数有关(因此需要调用 的析构函数)。
回答by James Kanze
It's explicitly legal. The rule is that the types used to instantiate
a template in the standard library must be complete, unlessotherwise
specified. In the case of unique_ptr
, §20.7.1/5 says “[...] The
template parameter T of unique_ptr may be an incomplete type.”
这显然是合法的。规则是用于在标准库中实例化模板的类型必须是完整的,除非另有说明。在 的情况下unique_ptr
,第 20.7.1/5 节说“[...] unique_ptr 的模板参数 T 可能是不完整的类型。
There are certain operations on the pointer which require a complete
type; in particular, when the object will actually be destructed (at
least with the default deleter). In your example, for example, if
A::~A()
were inline, this might cause problems. (Note that if you
don't declare the destructor yourself, it will be inline. Which
partially defeats the purpose of using std::unique_ptr
.)
指针上的某些操作需要完整的类型;特别是,当对象实际上将被破坏时(至少使用默认删除器)。例如,在您的示例中,如果
A::~A()
是内联的,这可能会导致问题。(请注意,如果您自己不声明析构函数,它将是内联的。这在一定程度上违背了使用 的目的std::unique_ptr
。)