Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++核心准则​T.84:使用非模板核心实现提供稳定的ABI接口

C++核心准则​T.84:使用非模板核心实现提供稳定的ABI接口

作者头像
面向对象思考
发布于 2020-09-28 08:38:03
发布于 2020-09-28 08:38:03
51900
代码可运行
举报
运行总次数:0
代码可运行

T.84: Use a non-template core implementation to provide an ABI-stable interface

T.84:使用非模板核心实现提供稳定的ABI接口

Reason(原因)

Improve stability of code. Avoid code bloat.

提高代码的稳定性。避免代码膨胀。

Example(示例)

It could be a base class:

它可以作为基类存在:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
struct Link_base {   // stable
    Link_base* suc;
    Link_base* pre;
};

template<typename T>   // templated wrapper to add type safety
struct Link : Link_base {
    T val;
};

struct List_base {
    Link_base* first;   // first element (if any)
    int sz;             // number of elements
    void add_front(Link_base* p);
    // ...
};

template<typename T>
class List : List_base {
public:
    void put_front(const T& e) { add_front(new Link<T>{e}); }   // implicit cast to Link_base
    T& front() { static_cast<Link<T>*>(first).val; }   // explicit cast back to Link<T>
    // ...
};

List<int> li;
List<string> ls;

Now there is only one copy of the operations linking and unlinking elements of a List. The Link and List classes do nothing but type manipulation.

(虽然例示了两个List类,)对于List的关联和非关联元素来讲,只有一套操作(函数)的拷贝。Link和List除了类型操作之外不做任何事。

Instead of using a separate "base" type, another common technique is to specialize for void or void* and have the general template for T be just the safely-encapsulated casts to and from the core void implementation.

除了使用独立的“基础”类型,另外一个通用技术是定义基于void和void*类型的核心实现并准备一个目的仅限于安全地封装从或到void核心实现进行转换的通用模板类。

Alternative: Use a Pimpl implementation.

其他选项:使用指向实现的指针技术来实现。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t84-use-a-non-template-core-implementation-to-provide-an-abi-stable-interface

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-09-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C++核心准则T.3:使用模板表现容器和范围
Containers need an element type, and expressing that as a template argument is general, reusable, and type safe. It also avoids brittle or inefficient workarounds. Convention: That's the way the STL does it.
面向对象思考
2020/08/27
2760
C++核心准则T.3:使用模板表现容器和范围
C++核心准则T.80:不要天真地模板化类继承
T.80: Do not naively templatize a class hierarchy
面向对象思考
2020/09/21
4730
C++核心准则Per.7:设计要为优化做准备
Because we often need to optimize the initial design. Because a design that ignores the possibility of later improvement is hard to change.
面向对象思考
2020/06/24
4710
C++核心准则T.42:使用模板别名简化记法并隐藏实现细节
Improved readability. Implementation hiding. Note that template aliases replace many uses of traits to compute a type. They can also be used to wrap a trait.
面向对象思考
2020/09/10
5850
C++核心准则C.100:定义容器时遵从STL标准‍
C.100: Follow the STL when defining a container
面向对象思考
2020/03/25
3340
C++核心准则T.65:使用标签分发提供函数的不同实现
T.65: Use tag dispatch to provide alternative implementations of a function
面向对象思考
2020/09/21
1.1K0
C++核心准则C.120:类层次体系只用于表现固有的阶层结构‍
C.120: Use class hierarchies to represent concepts with inherent hierarchical structure (only)
面向对象思考
2020/03/25
4040
C++核心准则C.60: 拷贝赋值运算符应该是以const&为参数,返回非常量引用类型的非虚函数
C.60: Make copy assignment non-virtual, take the parameter by const&, and return by non-const& C.60: 拷贝赋值运算符应该是以const&为参数,返回非常量引用类型的非虚函数
面向对象思考
2020/03/25
9830
C++核心准则ES.64:使用T{e}记法构造对象
The T{e} construction syntax makes it explicit that construction is desired. The T{e} construction syntax doesn't allow narrowing. T{e} is the only safe and general expression for constructing a value of type T from an expression e. The casts notations T(e) and (T)e are neither safe nor general.
面向对象思考
2020/05/29
6070
C++核心准则ES.64:使用T{e}记法构造对象
C++ 中文周刊 2024-11-16 第172期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2024-11-13 第280期
王很水
2024/11/18
1380
C++ 中文周刊 2024-11-16 第172期
C++核心准则C.129:设计类层次关系时,区分实现继承和接口继承‍
C.129: When designing a class hierarchy, distinguish between implementation inheritance and interface inheritance
面向对象思考
2020/03/25
5510
C++核心准则Con.2:默认情况下,将成员函数定义为const类型
A member function should be marked const unless it changes the object's observable state. This gives a more precise statement of design intent, better readability, more errors caught by the compiler, and sometimes more optimization opportunities.
面向对象思考
2020/08/18
7770
C++知识点
还没有整理过的笔记,有点乱 C++ 程序设计 II 兼谈对象模型 Conversion function - 转换函数 operator type() // this type -> other type class Fraction { // 分数类,分数可以被看成 double public: Fraction(int num, int den = 1) : m_numerator(num), m_denominator(den) {} operator double() co
yhlin
2023/02/13
9710
C++知识点
C++核心准则T.47:避免使用通用名称的高度不受限模板
An unconstrained template argument is a perfect match for anything so such a template can be preferred over more specific types that require minor conversions. This is particularly annoying/dangerous when ADL is used. Common names make this problem more likely.
面向对象思考
2020/09/10
5040
C++核心准则CP.42:不要在线程中无条件等待
A wait without a condition can miss a wakeup or wake up simply to find that there is no work to do.
面向对象思考
2020/07/16
8020
C++核心准则CP.42:不要在线程中无条件等待
C++核心准则T.62:将非依赖类模板成员放入非模板基类中
T.62: Place non-dependent class template members in a non-templated base class
面向对象思考
2020/09/21
9290
C++核心准则T.5:结合使用泛型和面向对象技术应该增强它们的效果而不是成本
Generic and OO techniques are complementary.
面向对象思考
2020/08/27
7050
C++核心准则T.5:结合使用泛型和面向对象技术应该增强它们的效果而不是成本
C++核心准则讨论:使用模板来表达容器(和其他资源句柄)
To provide statically type-safe manipulation of elements.
面向对象思考
2020/12/31
3100
C++核心准则讨论:使用模板来表达容器(和其他资源句柄)
C++核心准则C.122:需要完全隔离接口和实现时用抽象类作为接口‍
C.122: Use abstract classes as interfaces when complete separation of interface and implementation is needed
面向对象思考
2020/03/25
3840
C++核心准则​:注释风格
Compilers do not read comments. Comments are less precise than code. Comments are not updated as consistently as code.
面向对象思考
2020/11/26
5470
推荐阅读
相关推荐
C++核心准则T.3:使用模板表现容器和范围
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验