4

定义以下类时

class Foo 
{
public:
    Foo (void);
    ~Foo (void) = default;
protected:
    class FooImpl;
    std::unique_ptr <FooImpl> _impl;
//...
};

Foo::Foo (void) : _impl (std::make_unique <Foo> ()) {
}

我收到以下错误(icpc):

/home/toolworks/gcc/4.8.2/bin/../include/c++/4.8.2/bits/unique_ptr.h(65): 错误:不允许不完整的类型 static_assert(sizeof(_Tp)>0, ^在以下期间检测到:

“void std::default_delete<_Tp>::operator()(_Tp *) const [with _Tp=FooImpl]”在第 184 行实例化“std::unique_ptr<_Tp, _Dp>::~unique_ptr() [在“/home/toolworks/gcc/4.8.2/bin/../include/c++/4.8.2/bits/shared_ptr_base.h”的第 290 行使用 _Tp=FooImpl, _Dp=std::default_delete]" 实例化“void std::_Sp_counted_ptr<_Ptr, _Lp>::_M_dispose() [with _Ptr=Foo *, _Lp=__gnu_cxx::_S_atomic]”在“/home/toolworks/gcc/4.8.2/bin/”的第 286 行。 ./include/c++/4.8.2/bits/shared_ptr_base.h" 隐式生成“std::_Sp_counted_ptr<_Ptr, _Lp>::~_Sp_counted_ptr() [with _Ptr=Foo *, _Lp=__gnu_cxx::_S_atomic]”在“/home/toolworks/gcc/4.8.2/bin/../include/c++/4.8.2/bits/shared_ptr_base.h”的第 286 行在“/home/toolworks/gcc/4.8.2/bin/../include /c++/4.8.2/bits/shared_ptr_base.h”在第 452 行对“std::_Sp_counted_ptr<_Ptr, _Lp>::_Sp_counted_ptr(_Ptr) [with _Ptr=Foo *, _Lp=__gnu_cxx::_S_atomic]”进行实例化“/home/toolworks/gcc/4.8.2/bin/../include/c++/4.8.2/bits/shared_ptr_base.h” 实例化“std::__shared_count<_Lp>::__shared_count(_Ptr) [with _Lp =__gnu_cxx::_S_atomic, _Ptr=Foo *]" 在 "/home/toolworks/gcc/4.8.2/bin/../include/c++/4.8.2/bits/shared_ptr_base.h" 的第 740 行实例化 " std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1 *) [with _Tp=Foo, _Lp=__gnu_cxx::_S_atomic,_Tp1=Foo]”在“/home/toolworks/gcc/4.8.2/bin/../include/c++/4.8.2/bits/shared_ptr.h”的第113行“std::shared_ptr<_Tp> ::shared_ptr(_Tp1 *) [with _Tp=Foo, _Tp1=Foo]" at line ... of "main.cc"

但是,当我定义一个非默认析构函数时,它会编译:

Foo.h

class Foo 
{
public:
    Foo (void);
    ~Foo (void);
protected:
    class FooImpl;
    std::unique_ptr <FooImpl> _impl;
//...
};

Foo.cc

Foo::~Foo (void) {

} 

它编译。我在某些地方看到说“=default”应该编译,它与隐式默认函数不是同一个类(我是c++11的新手)。

那么,为什么第一个 Foo 不编译?

注意:我没有看到它是如何复制的,因为我询问了默认析构函数,而不是默认析构函数

4

0 回答 0