为什么 C++ 库和框架从不使用智能指针? [英] Why do C++ libraries and frameworks never use smart pointers?

查看:60
本文介绍了为什么 C++ 库和框架从不使用智能指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几篇文章中读到,几乎不应该使用原始指针.相反,它们应该始终包含在智能指针中,无论是作用域指针还是共享指针.

I read in a few articles that raw pointers should almost never be used. Instead they should always be wrapped inside smart pointers, whether it's scoped or shared pointers.

然而,我注意到像 Qt、wxWidgets 这样的框架和像 Boost 这样的库从不返回也不期待智能指针,就好像它们根本没有使用它们一样.相反,它们返回或期望原始指针.有什么理由吗?编写公共 API 时是否应该远离智能指针,为什么?

However, I noticed that frameworks like Qt, wxWidgets and libraries like Boost never return nor expect smart pointers, as if they were not using them at all. Instead, they return or expect raw pointers. Is there any reason for that? Should I stay away from smart pointers when I write a public API, and why?

只是想知道为什么在许多主要项目似乎都避免使用智能指针的情况下推荐使用智能指针.

Just wondering why smart pointers are recommended when many major projects seem to avoid them.

推荐答案

除了许多库是在标准智能指针出现之前编写的,最大的原因可能是缺乏标准的 C++ 应用程序二进制接口 (ABI)).

Apart from the fact that many libraries were written before the advent of standard smart pointers, the biggest reason is probably the lack of a standard C++ Application Binary Interface (ABI).

如果您正在编写一个只有头文件的库,您可以将智能指针和标准容器传递给您的核心内容.它们的源代码在编译时可供您的库使用,因此您只依赖于它们接口的稳定性,而不是它们的实现.

If you’re writing a header-only library, you can pass around smart pointers and standard containers to your heart’s content. Their source is available to your library at compile time, so you rely on the stability of their interfaces alone, not of their implementations.

但由于缺乏标准 ABI,您通常不能安全地跨模块边界传递这些对象.GCC shared_ptr 可能不同于 MSVC shared_ptr,后者也可能不同于 Intel shared_ptr.即使使用相同编译器,也不能保证这些类在版本之间是二进制兼容的.

But because of the lack of standard ABI, you generally cannot pass these objects safely across module boundaries. A GCC shared_ptr is probably different from an MSVC shared_ptr, which too can differ from an Intel shared_ptr. Even with the same compiler, these classes are not guaranteed to be binary compatible between versions.

最重要的是,如果您想发布一个预构建版本的库,您需要一个可依赖的标准 ABI.C 没有,但编译器供应商非常擅长给定平台的 C 库之间的互操作性 - 有事实上的标准.

The bottom line is that if you want to distribute a prebuilt version of your library, you need a standard ABI on which to rely. C doesn’t have one, but compiler vendors are very good about interoperability between C libraries for a given platform—there are de facto standards.

情况对 C++ 来说没有那么好.各个编译器可以处理它们自己的二进制文件之间的互操作,因此您可以选择为每个受支持的编译器分发一个版本,通常是 GCC 和 MSVC.但鉴于此,大多数库只导出 C 接口——这意味着原始指针.

The situation is not as good for C++. Individual compilers can handle interoperation between their own binaries, so you have the option of distributing a version for every supported compiler, often GCC and MSVC. But in light of this, most libraries just export a C interface—and that means raw pointers.

然而,非库代码通常应该更喜欢智能指针而不是原始代码.

Non-library code should, however, generally prefer smart pointers over raw.

这篇关于为什么 C++ 库和框架从不使用智能指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆