I.27: For stable library ABI, consider the Pimpl idiom(考虑使用指向实现的指针技术获得稳定的ABI)

Reason(原因)

Because private data members participate in class layout and private member functions participate in overload resolution, changes to those implementation details require recompilation of all users of a class that uses them. A non-polymorphic interface class holding a pointer to implementation (Pimpl) can isolate the users of a class from changes in its implementation at the cost of an indirection.

私有数据成员参与(内存上的)类布局,私有成员函数参加重载判断,因此改变这些实现细节需要重新编译类的所有用户。一个非多态的接口类管理一个指向实现的指针(Pimpl)可以通过一次间接访问的代价消除修改类实现对类用户的影响。

译者注:形式上很像桥接模式,但目的是为了隐藏而不是多态。

Example(示例)

interface (widget.h)

class widget {    class impl;    std::unique_ptr<impl> pimpl;public:    void draw(); // public API that will be forwarded to the implementation    widget(int); // defined in the implementation file    ~widget();   // defined in the implementation file, where impl is a complete type    widget(widget&&) = default;    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 datapublic:    void draw(const widget& w) { /* ... */ }    impl(int n) : n(n) {}};void widget::draw() { pimpl->draw(*this); }widget::widget(int n) : pimpl{std::make_unique<impl>(n)} {}widget::~widget() = default;widget& widget::operator=(widget&&) = default;

Notes(注意)

See GOTW #100 and cppreference for the trade-offs and additional implementation details associated with this idiom.

关于这个用法平衡考虑和另外的实现细节可以参考GOTW #100和cpprefernce网站。

译者注:以下是文中提到的两个参考网页。

GOTW#100:https://herbsutter.com/gotw/_100/

cppreference:https://en.cppreference.com/w/cpp/language/pimpl

Enforcement(实施建议)

(Not enforceable) It is difficult to reliably identify where an interface forms part of an ABI.

(无法强制)很难可靠地识别哪里的接口定义会成为ABI的一部分。

译者注:也就无法决定是否需要提示用户。

觉得本文有帮助,欢迎点赞并分享给更多的朋友!

更新文章,请关注微信公众号【面向对象思考】

C++核心准则边译边学-I.27 考虑使用指向实现的指针技术获得稳定的ABI相关推荐

  1. C++核心准则边译边学-目标之外

    In.not: Non-aims(目标之外) The rules are not intended to be minimal or orthogonal. In particular, genera ...

  2. C++核心准则边译边学-I.7 说明后置条件

    I.7: State postconditions (说明后置条件) Reason(原因) To detect misunderstandings about the result and possi ...

  3. C++核心准则边译边学-P.12: 酌情使用支持工具

    P.12: Use supporting tools as appropriate(酌情使用支持工具) Reason(原因) There are many things that are done b ...

  4. C++核心准则边译边学-I.6 表达前提条件最好使用Expects()

    I.6: Prefer Expects() for expressing preconditions 表达前提条件最好使用Expects() Reason(原因) To make it clear t ...

  5. C++核心准则边译边学-P.9 不要浪费时间和空间

    P.9: Don't waste time or space(不要浪费时间和空间) Reason(原因) This is C++. 我们在用C++. 译者注:之所以选择C++而不是其他语言,就是希望使 ...

  6. C++核心准则边译边学-I.8 表示后置条件最好使用Ensures()

    I.8: Prefer Ensures() for expressing postconditions(表示后置条件最好使用Ensures()) Reason(原因) To make it clear ...

  7. C++核心准则R.2: 只在接口中表示单独对象使用原始指针​

    R.2: In interfaces, use raw pointers to denote individual objects (only) R.2: 只在接口中表示单独对象使用原始指针 Reas ...

  8. modbus软件开发实战指南_C++核心准则?GSL:指南支持库

    GSL: Guidelines support library GSL:指南支持库 The GSL is a small library of facilities designed to suppo ...

  9. php 语法 条件变量,C ++核心准则:注意条件变量的陷阱

    今天,我写了一篇关于条件变量的恐怖文章.您应该意识到条件变量的这一问题.C ++核心准则CP 42仅声明:"不要无条件等待". 等待!条件变量支持一个非常简单的概念.一个线程准备一 ...

最新文章

  1. 齐博cms 7.0 漏洞分析
  2. linux 条件语句关键词,Linux中条件语句
  3. Redis Labs 再次更改开源许可证,但 Redis 本身不受影响
  4. Pthread多线程编程之查看Pthread版本的方法
  5. VS中的调试相关的技巧
  6. 计算机二级考试Access教程
  7. Spring框架知识复习之二
  8. linux 查看tomcat启动_一次tomcat启动特别慢的排查
  9. 《QML 基础教程》
  10. vue 图片放大查看器
  11. C#用Socket和S7.net实现安卓手机APP读写西门子PLC数据(安卓APP使用的E4A中文编程软件)
  12. 社交网络分析(Social Network Analysis)
  13. sata硬盘系统迁移到nvme协议固态硬盘后开机蓝屏 蓝屏代码inaccessible_boot_device无限重启问题解决
  14. 在上海乐字节学习CRM项目管理
  15. 魂斗罗进化革命+塞班JAVA版_魂斗罗进化革命电脑版
  16. Reservior Sampling (蓄水池抽样算法)
  17. Linux0号进程,1号进程,2号进程
  18. 数学与应用数学专业考计算机研究生,数学与应用数学专业学生考研有哪些方向 数学与应用数学专业学生考研方向是怎样的...
  19. 从ICML 2022看域泛化(Domain Generalization)最新进展
  20. J1939-21数据链路层多帧协议PDU解析

热门文章

  1. 2.1 随机变量及其分布
  2. 弘辽科技:B站携手阿里入股如涵 UP主里要诞生下一个李佳琦?
  3. Navicat Premium和Navicat for MySQL的区别
  4. 机器学习实战- 回归(Regression) 概述
  5. c语言编程a4988驱动步进电机,A4988 步进电机驱动模块测试
  6. 三极管的经典之作,你知道吗?
  7. 一阶IIR数字滤波器的设计
  8. chp2-2-2_fmm_word_seg通过最大正向匹配算法对句子进行切分
  9. 报错解决:PermissionError
  10. jtag、在线仿真器