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

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

作者头像
面向对象思考
发布于 2020-09-28 08:38:03
发布于 2020-09-28 08:38:03
51300
代码可运行
举报
运行总次数: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++核心准则​讨论:如果在初始化期间需要“虚行为”,请使用工厂函数
If your design wants virtual dispatch into a derived class from a base class constructor or destructor for functions like f and g, you need other techniques, such as a post-constructor -- a separate member function the caller must invoke to complete initialization, which can safely call f and g because in member functions virtual calls behave normally. Some techniques for this are shown in the References. Here's a non-exhaustive list of options:
面向对象思考
2020/12/15
5050
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
2680
C++核心准则T.3:使用模板表现容器和范围
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
4650
C++核心准则T.80:不要天真地模板化类继承
T.80: Do not naively templatize a class hierarchy
面向对象思考
2020/09/21
4620
C++核心准则C.100:定义容器时遵从STL标准‍
C.100: Follow the STL when defining a container
面向对象思考
2020/03/25
3270
C++核心准则ES.23:优先使用{}​初始化器语法
Prefer {}. The rules for {} initialization are simpler, more general, less ambiguous, and safer than for other forms of initialization.
面向对象思考
2020/04/28
6110
C++核心准则ES.23:优先使用{}​初始化器语法
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
5720
C++核心准则​:注释风格
Compilers do not read comments. Comments are less precise than code. Comments are not updated as consistently as code.
面向对象思考
2020/11/26
5340
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
6000
C++核心准则ES.64:使用T{e}记法构造对象
C++ 中文周刊 2024-11-16 第172期
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2024-11-13 第280期
王很水
2024/11/18
1210
C++ 中文周刊 2024-11-16 第172期
C++核心准则T.61:不要过度参数化成员(SCARY)
T.61: Do not over-parameterize members (SCARY)
面向对象思考
2020/09/21
2900
C++核心准则C.120:类层次体系只用于表现固有的阶层结构‍
C.120: Use class hierarchies to represent concepts with inherent hierarchical structure (only)
面向对象思考
2020/03/25
3940
C++核心准则C.129:设计类层次关系时,区分实现继承和接口继承‍
C.129: When designing a class hierarchy, distinguish between implementation inheritance and interface inheritance
面向对象思考
2020/03/25
5390
C++核心准则T.43: 定义别名时,using比typedef更好
Improved readability: With using, the new name comes first rather than being embedded somewhere in a declaration. Generality: using can be used for template aliases, whereas typedefs can't easily be templates. Uniformity: using is syntactically similar to auto.
面向对象思考
2020/09/10
4910
C++核心准则C.122:需要完全隔离接口和实现时用抽象类作为接口‍
C.122: Use abstract classes as interfaces when complete separation of interface and implementation is needed
面向对象思考
2020/03/25
3770
C++核心准则T.65:使用标签分发提供函数的不同实现
T.65: Use tag dispatch to provide alternative implementations of a function
面向对象思考
2020/09/21
1.1K0
C++核心准则R.3: 原始指针(T*)不应拥有所有权
There is nothing (in the C++ standard or in most code) to say otherwise and most raw pointers are non-owning. We want owning pointers identified so that we can reliably and efficiently delete the objects pointed to by owning pointers.
面向对象思考
2020/03/25
7250
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
4980
C++核心准则讨论:使用模板来表达容器(和其他资源句柄)
To provide statically type-safe manipulation of elements.
面向对象思考
2020/12/31
3050
C++核心准则讨论:使用模板来表达容器(和其他资源句柄)
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
7710
推荐阅读
相关推荐
C++核心准则​讨论:如果在初始化期间需要“虚行为”,请使用工厂函数
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档