当前位置:网站首页>Unique in Pimpl_ PTR compilation errors and Solutions
Unique in Pimpl_ PTR compilation errors and Solutions
2022-06-23 01:38:00 【summer_ sunrise】
Origin of problem
Realization Pimpl when , Use unique_ptr when , The compiler will report the following error :
[build] /usr/include/c++/9/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = widget::impl]’:
[build] /usr/include/c++/9/bits/unique_ptr.h:292:17: required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = widget::impl; _Dp = std::default_deletewidget::impl]’
[build] /usr/include/c++/9/experimental/propagate_const:63:11: required from here
[build] /usr/include/c++/9/bits/unique_ptr.h:79:16: error: invalid application of ‘sizeof’ to incomplete type ‘widget::impl’
[build] 79 | static_assert(sizeof(_Tp)>0,
[build] | ^~~~~~~~~~~
Sample code :
#include <iostream>
#include <memory>
#include <experimental/propagate_const>
// interface (widget.h)
class widget {
class impl;
std::experimental::propagate_const<std::unique_ptr<impl>> pImpl;
public:
void draw();
bool shown() const {
return true; } // public API that implementation has to call
widget(int);
widget(widget&&); // defined in the implementation file
// Note: calling draw() on moved-from object is UB
widget(const widget&) = delete;
widget& operator=(widget&&); // defined in the implementation file
widget& operator=(const widget&) = delete;
};
// implementation (widget.cpp)
class widget::impl {
int n; // private data
public:
void draw(const widget& w) {
if(w.shown())
std::cout << "drawing a non-const widget " << n << '\n';
}
impl(int n) : n(n) {
}
};
void widget::draw() const {
pImpl->draw(*this); }
void widget::draw() {
pImpl->draw(*this); }
widget::widget(int n) : pImpl{
std::make_unique<impl>(n)} {
}
widget::widget(widget&&) = default;
widget& widget::operator=(widget&&) = default;
// user (main.cpp)
int main()
{
widget w(7);
w.draw();
}
What is? Pimpl
Pimpl is pointer to implementation;
is a c++ programe technique: removes implementation details of a class from its objects representation by placing them in a separate class, accessed through on opaque pointer;
yes pointer to implementation Abbreviation ; It's a kind of c++ Programming technology , Move the detailed implementation of the class from its own object to a separate class , Then access these implementations through a pointer .
It is mainly used to construct stable ABI Library interface for , Reduce compile time dependencies ; Interface oriented programming , Using interface isolation will enable hiding .
The class diagram is as follows :
The reason for the error
about widget, The compiler will generate destructors for it by default , In its destructor, it calls pImpl Destructor of , Would call std::unique_ptr The analysis of ,unique_ptr The destructor of is the one that calls it deleter, default deleter The native pointer will be delete, That is to say delete impl; c++ Compiler default deleter Will be in delete front , Check to be delete The pointer to , Is the type complete ( That's the top static_assert(sizeof(_Tp)>0) The inspection of ), Because the destructor generated by the compiler by default is inline ( Also in .h Chinese compiler ), here impl It is also an object with incomplete type , because impl Is a forward statement .
resolvent
- Show declaration destructors , And put its implementation in impl Where the type is complete (cpp In file )
// interface (widget.h)
class widget {
class impl;
std::experimental::propagate_const<std::unique_ptr<impl>> pImpl;
public:
~widget();
widget(int);
};
// implementation (widget.cpp)
class widget::impl {
int n; // private data
public:
impl(int n) : n(n) {
}
};
widget::~widget() = default; // defined in the implementation file, where impl is a complete type
This solution is in Effective_Modern_C__ Of item 22(When using the Pimpl Idiom, define special
member functions in the implementation file)
- take std::unique_ptr Replace with shared_ptr
template< class T > class shared_ptr;
template<class T, class Deleter = std::default_delete<T>> class unique_ptr;
template <class T, class Deleter> class unique_ptr<T[], Deleter>;
The reason for this behavior is simple :unique_ptr Is a part of itself , and shared_ptr No . To ensure efficiency ,unique_ptr The delegator must guarantee raw pointer by complete, and shared_ptr This guarantee is not required .
- Customize deleter, And will deleter The implementation of is placed in cpp in
// interface (widget.h)
class widget {
class impl;
struct Deleter{
void operator()(impl*);
};
std::experimental::propagate_const<std::unique_ptr<impl, Deleter>> pImpl;
public:
widget(int);
};
// implementation (widget.cpp)
class widget::impl {
int n; // private data
public:
impl(int n) : n(n) {
}
};
void widget::Deleter::operator()(impl *p) {
delete p;
}
边栏推荐
- Pat a - 1010 radical (thinking + two points)
- Day260: the number III that appears only once
- Pat class A - 1012 the best rank (PIT)
- Yyds dry inventory solution sword finger offer: print the binary tree into multiple lines
- The road of architects starts from "storage selection"
- Extend your kubernetes API using the aggregation API
- Pat class A - 1014 waiting in line (bank queuing problem | queue+ simulation)
- Voice network multiplayer video recording and synthesis support offline re recording | Nuggets technology solicitation
- Debian10 create users, user groups, switch users
- E-R diagram
猜你喜欢

Using WordPress to create a MySQL based education website (learning notes 2) (technical notes 1) xampp error1045 solution

Webdriver and selenium Usage Summary

The road of architects starts from "storage selection"
![[learning notes] roll back Mo team](/img/19/d374dd172b9609a3f57de50791b19e.png)
[learning notes] roll back Mo team

MySQL-Seconds_ behind_ Master accuracy error
![[Title Fine brushing] 2023 Hesai FPGA](/img/e8/d9d8c549a4fee529b69ebfc9a9d6ef.png)
[Title Fine brushing] 2023 Hesai FPGA

SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications

崔鹏团队:万字长文梳理「稳定学习」全景图

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

Autumn move script C
随机推荐
Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data
Module 8 job
Day260: the number III that appears only once
Extend your kubernetes API using the aggregation API
On AI and its future trend | community essay solicitation
Node fetch download file
SQL programming task03 job - more complex query
Population standard deviation and sample standard deviation
Lexical Sign Sequence
Pat class A - 1013 battle over cities
Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
[cmake command notes]target_ compile_ options
C#. Net universal database access encapsulation classes (access, sqlserver, Oracle)
Modulenotfounderror: no module named 'rospy', PIP could not find the installation package
Nuxt - auto generate dynamic route bug
崔鹏团队:万字长文梳理「稳定学习」全景图
Day575: divide candy
Ansible learning summary (8) -- Summary of ansible control right raising related knowledge
Quick sort method
Sélecteur de hiérarchie