相关疑难解决方法(0)

具有不完整类型的std :: unique_ptr将无法编译

我正在使用pimpl-idiom std::unique_ptr:

class window {
  window(const rectangle& rect);

private:
  class window_impl; // defined elsewhere
  std::unique_ptr<window_impl> impl_; // won't compile
};
Run Code Online (Sandbox Code Playgroud)

但是,我在第304行的第304行收到有关使用不完整类型的编译错误<memory>:

' sizeof'到不完整类型' uixx::window::window_impl的应用无效' '

据我所知,std::unique_ptr应该可以使用不完整的类型.这是libc ++中的错误还是我在这里做错了什么?

c++ unique-ptr incomplete-type libc++

179
推荐指数
5
解决办法
5万
查看次数

为什么 = 默认成员初始值设定项请求 unique_ptr 析构函数的实例化,而 {} 则不然?

这是这个问题的后续:Does PIMPL idiom 实际上可以使用 std::unique_ptr?

完整的示例使用多个文件,因此为了解决这个问题,我将在这里减少它。完整的工作示例在这里: https: //wandbox.org/permlink/AepAJYkbRU4buDoJ,完整的非工作示例在这里:https://wandbox.org/permlink/0kP23UYJbSaUvJgS

更短的例子是:

#include <memory>

struct A;

struct B {
    ~B();
    std::unique_ptr<A> p =  nullptr;
};
Run Code Online (Sandbox Code Playgroud)

会产生错误

In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/memory:76:
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/unique_ptr.h:83:16: error: invalid application of 'sizeof' to an incomplete type 'A'
        static_assert(sizeof(_Tp)>0,
                      ^~~~~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/unique_ptr.h:361:4: note: in instantiation of member function 'std::default_delete<A>::operator()' requested here
          get_deleter()(std::move(__ptr));
          ^
<source>:7:29: note: in instantiation of member function 'std::unique_ptr<A>::~unique_ptr' requested here
    std::unique_ptr<A> p =  nullptr;
                            ^
<source>:3:8: …
Run Code Online (Sandbox Code Playgroud)

c++ pimpl-idiom unique-ptr language-lawyer template-instantiation

16
推荐指数
1
解决办法
545
查看次数