为什么c++库和框架从不使用智能指针?

时间:2022-10-06 10:32:44

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.

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

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?

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

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

当许多大型项目似乎都避免使用智能指针时,你会感到奇怪。

8 个解决方案

#1


120  

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).

除了在标准智能指针出现之前编写了许多库之外,最大的原因可能是缺少标准c++应用程序二进制接口(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.

如果您正在编写一个仅用于头部的库,您可以将智能指针和标准容器传递到您的心脏内容。它们的源代码在编译时对库可用,因此您只能依赖于它们接口的稳定性,而不是它们的实现。

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,您通常无法跨模块边界安全地传递这些对象。GCC shared_ptr可能与MSVC shared_ptr不同,后者也可能与Intel shared_ptr不同。即使使用相同的编译器,这些类也不能保证在版本之间是二进制兼容的。

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.

底线是,如果您想要发布您的库的预构建版本,您需要一个标准的ABI来依赖它。C没有,但是编译器供应商非常擅长在给定平台的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.

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

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

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

#2


40  

There can be many reasons. To list few of them:

有很多原因。列举其中的一些:

  1. Smart pointers became part of standard just recently. Till then they were part of other libraries
  2. 智能指针最近成为了标准指针的一部分。在那之前,它们是其他图书馆的一部分
  3. Their primary use is to avoid memory leaks; many libraries don't have their own memory management; Generally they provide utilities and APIs
  4. 它们的主要用途是避免内存泄漏;许多库没有自己的内存管理;通常,它们提供实用程序和api。
  5. They are implemented as wrapper, since they are actually objects and not pointers. Which has additional time/space cost, compared to raw pointers; The users of the libraries may not want to have such overheads
  6. 它们被实现为包装器,因为它们实际上是对象而不是指针。与原始指针相比,它有额外的时间/空间成本;这些库的用户可能不希望有这样的管理费用

Edit: Using smart pointers is a completely developer's choice. It depends on various factors.

编辑:使用智能指针完全是开发人员的选择。这取决于各种因素。

  1. In performance critical systems, you may not want to use smart pointers which generates overhead

    在性能关键系统中,您可能不希望使用智能指针来产生开销

  2. The project which needs the backward compatibility, you may not want to use the smart pointers which has C++11 specific features

    需要向后兼容性的项目,您可能不希望使用具有c++ 11特定特性的智能指针

Edit2 There is a string of several downvotes in the span of 24 hours because of below passage. I fail to understand why the answer is downvoted even though below is just an add-on suggestion and not an answer.
However, C++ always facilitates you to have the options open. :) e.g.

由于下面的段落,在24小时内有一连串的下降。我无法理解为什么答案被否决,尽管下面只是一个附加的建议,而不是一个答案。但是,c++总是帮助您打开选项。如:)。

template<typename T>
struct Pointer {
#ifdef <Cpp11>
  typedef std::unique_ptr<T> type;
#else
  typedef T* type;
#endif
};

And in your code use it as:

在你的代码中使用它:

Pointer<int>::type p;

For those who say that a smart pointer and a raw pointer are different, I agree with that. The code above was just an idea where one can write a code which is interchangeable just with a #define, this is not compulsion;

对于那些说智能指针和原始指针不同的人,我同意这一点。上面的代码只是一个想法,一个人可以编写一个代码,这个代码可以用#define来交换,这不是强制的;

For example, T* has to be deleted explicitly but a smart pointer does not. We can have a templated Destroy() to handle that.

例如,必须显式地删除T*,但是智能指针不会。我们可以使用模板Destroy()来处理这个问题。

template<typename T>
void Destroy (T* p)
{
  delete p;
}
template<typename T>
void Destroy (std::unique_ptr<T> p)
{
  // do nothing
}

and use it as:

并使用它:

Destroy(p);

In the same way, for a raw pointer we can copy it directly and for smart pointer we can use special operation.

同样,对于原始指针,我们可以直接复制它,对于智能指针,我们可以使用特殊的操作。

Pointer<X>::type p = new X;
Pointer<X>::type p2(Assign(p));

Where Assign() is as:

在分配():

template<typename T>
T* Assign (T *p)
{
  return p;
}
template<typename T>
... Assign (SmartPointer<T> &p)
{
  // use move sematics or whateve appropriate
}

#3


35  

There are two issues with smart pointers (pre C++11):

智能指针有两个问题(pre - c++ 11):

  • non-standards, so each library tend to reinvent its own (NIH syndrom & dependencies issues)
  • 非标准,所以每个图书馆都倾向于重新创建自己的(NIH syndrome & dependencies问题)
  • potential cost
  • 潜在的成本

The default smart pointer, in that it is cost-free, is unique_ptr. Unfortunately it requires C++11 move semantics, which only appeared recently. All other smart pointers have a cost (shared_ptr, intrusive_ptr) or have less than ideal semantics (auto_ptr).

默认的智能指针是unique_ptr,因为它是免费的。不幸的是,它需要c++ 11的移动语义,这是最近才出现的。所有其他智能指针都有代价(shared_ptr、入侵者sive_ptr)或语义不太理想(auto_ptr)。

With C++11 around the corner, bringing a std::unique_ptr, one would be tempted to think that it is finally over... I am not so optimistic.

随着c++ 11的出现,带来了std::unique_ptr,人们可能会认为它终于结束了……我不是很乐观。

Only a few major compilers implement most of C++11, and only in their recent versions. We can expect major libraries such as QT and Boost to be willing to retain compatibility with C++03 for a while, which somewhat precludes the wide adoption of the new and shiny smart pointers.

只有少数几个主要的编译器实现了c++ 11的大部分,而且只在最近的版本中实现。我们可以预期,QT和Boost等主要库愿意暂时保持与c++ 03的兼容性,这在一定程度上妨碍了广泛采用新的、闪亮的智能指针。

#4


12  

You shouldn't stay away from smart pointers, they have their use especially in applications where you have to pass a object around.

你不应该远离智能指针,它们有它们的用途,尤其是在需要传递对象的应用程序中。

Libraries tend to either just return a value or populate a object. They don't usually have objects that need to be used in a lot of places, so there is no need for them to use smart pointers (at least not in their interface, they may use them internally).

库通常要么返回一个值,要么填充一个对象。它们通常没有需要在很多地方使用的对象,因此它们不需要使用智能指针(至少在它们的接口中,它们可能在内部使用)。

I could take as example a library we have been working on, where after a few months of development I realized we only used pointers and smart pointers in a few classes (3-5% of all classes).

我可以以我们正在开发的一个库为例,在几个月的开发之后,我意识到我们只在一些类中使用指针和智能指针(所有类的3-5%)。

Passing variables by reference was enough in most places, we used smart pointers whenever we had a object that could be null, and raw pointers when a library that we used forced us to.

在大多数情况下,通过引用传递变量就足够了,当我们有一个可以为空的对象时,我们使用智能指针;当我们使用的库强迫我们使用原始指针时,我们使用原始指针。

Edit (I can't comment because of my reputation): passing variables by reference is very flexible: if you want the object to be readonly you can use a const reference (you can still do some nasty casts to be able to write the object) but you get the maximum of protection possible (it's the same with smart pointers). But I do agree that it's much nicer to just return the object.

编辑(我不能评论,因为我的声誉):通过变量通过引用非常灵活:如果你想要的对象是只读的,您可以使用一个常量引用(你仍然可以做一些讨厌的投能写对象)但你得到最大的保护可能与智能指针(这是相同的)。但我同意,返回对象会更好。

#5


9  

Qt pointlessly re-invented many parts of the Standard library in an attempt to become Java. I believe that it does actually have its own smart pointers now, but in general, it is hardly a pinnacle of design. wxWidgets, as far as I'm aware, was designed long before usable smart pointers were written.

Qt毫无意义地重新设计了标准库的许多部分,试图成为Java。我相信它现在确实有自己的智能指针,但总的来说,它并不是设计的顶峰。据我所知,wxWidgets早在编写可用的智能指针之前就被设计出来了。

As for Boost, I fully expect that they use smart pointers wherever appropriate. You might have to be more specific.

至于Boost,我完全期望它们在适当的地方使用智能指针。你可能需要更具体一些。

In addition, don't forget that smart pointers exist to enforce ownership. If the API has no ownership semantics, then why use a smart pointer?

此外,不要忘记,有一些智能指针是用来加强所有权的。如果API没有所有权语义,那么为什么要使用智能指针呢?

#6


3  

Good question. I don't know the specific articles to which you refer, but I have read similar things from time to time. My suspicion is that the writers of such articles tend to harbor a bias against C++-style programming. If the writer programs in C++ only when he must, then returns to Java or such as soon as he can, then he doesn't really share the C++ mindset.

好问题。我不知道你所指的具体文章是什么,但我时常读到类似的东西。我怀疑这类文章的作者倾向于对c++风格的编程抱有偏见。如果作者只在必须的时候才用c++编程,然后尽可能快地返回Java或类似的东西,那么他就没有真正的c++思维。

One suspects that some or most of the same writers prefer garbage-collecting memory managers. I don't, but I think differently than they do.

有人怀疑,同样的一些或大多数作者更喜欢垃圾收集内存管理器。我不知道,但我的想法和他们不一样。

Smart pointers are great, but they have to keep reference counts. The keeping of reference counts bears costs -- often modest costs, but costs nonetheless -- at runtime. There is nothing wrong with saving these costs by using bare pointers, especially if the pointers are managed by destructors.

聪明的指针是很棒的,但是它们必须保持引用计数。保持参考计数的成本——通常是适度的成本,但无论如何——在运行时是要付出代价的。通过使用裸指针来节省这些成本没有错,特别是如果指针是由析构函数管理的。

One of the excellent things about C++ is its support for embedded-systems programming. The use of bare pointers is part of that.

c++的优秀之处在于它对嵌入式系统编程的支持。使用裸指针是其中的一部分。

Update: A commenter has correctly observed that C++'s new unique_ptr (available since TR1) does not count references. The commenter also has a different definition of "smart pointer" than I have in mind. He may be right about the definition.

更新:一个评论者正确地观察到c++的新unique_ptr(从TR1开始可用)不计算引用。评论者对“智能指针”的定义也与我所想的不同。他的定义可能是对的。

Further update: The comment thread below is illuminating. All of it is recommended reading.

进一步更新:下面的注释线程很有启发性。所有这些都是推荐阅读。

#7


2  

There are also other types of smart pointers. You might want a specialized smart pointer for something like network replication (one that detects if it's accessed and sends any modifications to the server or some such), keeps a history of changes, marks the fact that it was accessed so it can be investigated when you save data to disk and so on. Not sure if doing that in the pointer is the best solution but using the built in smart pointer types in libraries could result in people being locked into them and loosing the flexibility.

还有其他类型的智能指针。你可能想要一个专门的智能指针类似网络复制(检测如果访问和向服务器发送任何修改或一些),不断变化的历史,标志着它,因此它可以访问调查当您保存数据到磁盘等等。不确定在指针中这样做是否是最好的解决方案,但是在库中使用内置的智能指针类型可能会导致人们被锁在指针中并失去灵活性。

People can have all kinds of different memory management requirements and solutions beyond smart pointers. I might want to manage memory myself, I could be allocating space for things in a memory pool so it's allocated in advance and not at runtime (useful for games). I might be using a garbage collected implementation of C++ (C++11 makes this possible although none exist yet). Or maybe I'm just not doing anything advanced enough to worry about bothering with them, I can know that I'm not going to forget to uninitialized objects and so on. Maybe I'm just confident in my ability to manage memory without the pointer crutch.

除了智能指针之外,人们还可以有各种不同的内存管理需求和解决方案。我可能想自己管理内存,我可以为内存池中的东西分配空间,所以它是预先分配的,而不是在运行时(对游戏有用)。我可能正在使用c++的垃圾收集实现(c++ 11使这成为可能,尽管还不存在)。或者我只是没有做足够高级的事情来担心它们,我知道我不会忘记未初始化的对象等等。也许我只是对自己在没有指针拐杖的情况下管理内存的能力有信心。

Integration with C is another issue too.

与C集成也是另一个问题。

Another issue is smart pointers are part of the STL. C++ is designed to be usable without the STL.

另一个问题是智能指针是STL的一部分。c++被设计成可以在没有STL的情况下使用。

#8


-1  

It also depends on what domain you work in. I write game engines for a living, we avoid boost like the plague, in games the overhead of boost isn't acceptable. In our core engine we ended up writing our own version of stl (Much like the ea stl).

这也取决于你在哪个领域工作。我以编写游戏引擎为生,我们像瘟疫一样避免升级,在游戏中,升级的开销是不可接受的。在我们的核心引擎中,我们最终编写了自己的stl版本(很像ea stl)。

If i was to write a forms application, i might consider using smart pointers; but once memory management is second nature not having granular control over memory becomes quiet annoying.

如果我要编写一个表单应用程序,我可能会考虑使用智能指针;但是一旦内存管理成为第二天性,对内存没有粒度控制就会变得非常烦人。

#1


120  

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).

除了在标准智能指针出现之前编写了许多库之外,最大的原因可能是缺少标准c++应用程序二进制接口(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.

如果您正在编写一个仅用于头部的库,您可以将智能指针和标准容器传递到您的心脏内容。它们的源代码在编译时对库可用,因此您只能依赖于它们接口的稳定性,而不是它们的实现。

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,您通常无法跨模块边界安全地传递这些对象。GCC shared_ptr可能与MSVC shared_ptr不同,后者也可能与Intel shared_ptr不同。即使使用相同的编译器,这些类也不能保证在版本之间是二进制兼容的。

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.

底线是,如果您想要发布您的库的预构建版本,您需要一个标准的ABI来依赖它。C没有,但是编译器供应商非常擅长在给定平台的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.

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

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

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

#2


40  

There can be many reasons. To list few of them:

有很多原因。列举其中的一些:

  1. Smart pointers became part of standard just recently. Till then they were part of other libraries
  2. 智能指针最近成为了标准指针的一部分。在那之前,它们是其他图书馆的一部分
  3. Their primary use is to avoid memory leaks; many libraries don't have their own memory management; Generally they provide utilities and APIs
  4. 它们的主要用途是避免内存泄漏;许多库没有自己的内存管理;通常,它们提供实用程序和api。
  5. They are implemented as wrapper, since they are actually objects and not pointers. Which has additional time/space cost, compared to raw pointers; The users of the libraries may not want to have such overheads
  6. 它们被实现为包装器,因为它们实际上是对象而不是指针。与原始指针相比,它有额外的时间/空间成本;这些库的用户可能不希望有这样的管理费用

Edit: Using smart pointers is a completely developer's choice. It depends on various factors.

编辑:使用智能指针完全是开发人员的选择。这取决于各种因素。

  1. In performance critical systems, you may not want to use smart pointers which generates overhead

    在性能关键系统中,您可能不希望使用智能指针来产生开销

  2. The project which needs the backward compatibility, you may not want to use the smart pointers which has C++11 specific features

    需要向后兼容性的项目,您可能不希望使用具有c++ 11特定特性的智能指针

Edit2 There is a string of several downvotes in the span of 24 hours because of below passage. I fail to understand why the answer is downvoted even though below is just an add-on suggestion and not an answer.
However, C++ always facilitates you to have the options open. :) e.g.

由于下面的段落,在24小时内有一连串的下降。我无法理解为什么答案被否决,尽管下面只是一个附加的建议,而不是一个答案。但是,c++总是帮助您打开选项。如:)。

template<typename T>
struct Pointer {
#ifdef <Cpp11>
  typedef std::unique_ptr<T> type;
#else
  typedef T* type;
#endif
};

And in your code use it as:

在你的代码中使用它:

Pointer<int>::type p;

For those who say that a smart pointer and a raw pointer are different, I agree with that. The code above was just an idea where one can write a code which is interchangeable just with a #define, this is not compulsion;

对于那些说智能指针和原始指针不同的人,我同意这一点。上面的代码只是一个想法,一个人可以编写一个代码,这个代码可以用#define来交换,这不是强制的;

For example, T* has to be deleted explicitly but a smart pointer does not. We can have a templated Destroy() to handle that.

例如,必须显式地删除T*,但是智能指针不会。我们可以使用模板Destroy()来处理这个问题。

template<typename T>
void Destroy (T* p)
{
  delete p;
}
template<typename T>
void Destroy (std::unique_ptr<T> p)
{
  // do nothing
}

and use it as:

并使用它:

Destroy(p);

In the same way, for a raw pointer we can copy it directly and for smart pointer we can use special operation.

同样,对于原始指针,我们可以直接复制它,对于智能指针,我们可以使用特殊的操作。

Pointer<X>::type p = new X;
Pointer<X>::type p2(Assign(p));

Where Assign() is as:

在分配():

template<typename T>
T* Assign (T *p)
{
  return p;
}
template<typename T>
... Assign (SmartPointer<T> &p)
{
  // use move sematics or whateve appropriate
}

#3


35  

There are two issues with smart pointers (pre C++11):

智能指针有两个问题(pre - c++ 11):

  • non-standards, so each library tend to reinvent its own (NIH syndrom & dependencies issues)
  • 非标准,所以每个图书馆都倾向于重新创建自己的(NIH syndrome & dependencies问题)
  • potential cost
  • 潜在的成本

The default smart pointer, in that it is cost-free, is unique_ptr. Unfortunately it requires C++11 move semantics, which only appeared recently. All other smart pointers have a cost (shared_ptr, intrusive_ptr) or have less than ideal semantics (auto_ptr).

默认的智能指针是unique_ptr,因为它是免费的。不幸的是,它需要c++ 11的移动语义,这是最近才出现的。所有其他智能指针都有代价(shared_ptr、入侵者sive_ptr)或语义不太理想(auto_ptr)。

With C++11 around the corner, bringing a std::unique_ptr, one would be tempted to think that it is finally over... I am not so optimistic.

随着c++ 11的出现,带来了std::unique_ptr,人们可能会认为它终于结束了……我不是很乐观。

Only a few major compilers implement most of C++11, and only in their recent versions. We can expect major libraries such as QT and Boost to be willing to retain compatibility with C++03 for a while, which somewhat precludes the wide adoption of the new and shiny smart pointers.

只有少数几个主要的编译器实现了c++ 11的大部分,而且只在最近的版本中实现。我们可以预期,QT和Boost等主要库愿意暂时保持与c++ 03的兼容性,这在一定程度上妨碍了广泛采用新的、闪亮的智能指针。

#4


12  

You shouldn't stay away from smart pointers, they have their use especially in applications where you have to pass a object around.

你不应该远离智能指针,它们有它们的用途,尤其是在需要传递对象的应用程序中。

Libraries tend to either just return a value or populate a object. They don't usually have objects that need to be used in a lot of places, so there is no need for them to use smart pointers (at least not in their interface, they may use them internally).

库通常要么返回一个值,要么填充一个对象。它们通常没有需要在很多地方使用的对象,因此它们不需要使用智能指针(至少在它们的接口中,它们可能在内部使用)。

I could take as example a library we have been working on, where after a few months of development I realized we only used pointers and smart pointers in a few classes (3-5% of all classes).

我可以以我们正在开发的一个库为例,在几个月的开发之后,我意识到我们只在一些类中使用指针和智能指针(所有类的3-5%)。

Passing variables by reference was enough in most places, we used smart pointers whenever we had a object that could be null, and raw pointers when a library that we used forced us to.

在大多数情况下,通过引用传递变量就足够了,当我们有一个可以为空的对象时,我们使用智能指针;当我们使用的库强迫我们使用原始指针时,我们使用原始指针。

Edit (I can't comment because of my reputation): passing variables by reference is very flexible: if you want the object to be readonly you can use a const reference (you can still do some nasty casts to be able to write the object) but you get the maximum of protection possible (it's the same with smart pointers). But I do agree that it's much nicer to just return the object.

编辑(我不能评论,因为我的声誉):通过变量通过引用非常灵活:如果你想要的对象是只读的,您可以使用一个常量引用(你仍然可以做一些讨厌的投能写对象)但你得到最大的保护可能与智能指针(这是相同的)。但我同意,返回对象会更好。

#5


9  

Qt pointlessly re-invented many parts of the Standard library in an attempt to become Java. I believe that it does actually have its own smart pointers now, but in general, it is hardly a pinnacle of design. wxWidgets, as far as I'm aware, was designed long before usable smart pointers were written.

Qt毫无意义地重新设计了标准库的许多部分,试图成为Java。我相信它现在确实有自己的智能指针,但总的来说,它并不是设计的顶峰。据我所知,wxWidgets早在编写可用的智能指针之前就被设计出来了。

As for Boost, I fully expect that they use smart pointers wherever appropriate. You might have to be more specific.

至于Boost,我完全期望它们在适当的地方使用智能指针。你可能需要更具体一些。

In addition, don't forget that smart pointers exist to enforce ownership. If the API has no ownership semantics, then why use a smart pointer?

此外,不要忘记,有一些智能指针是用来加强所有权的。如果API没有所有权语义,那么为什么要使用智能指针呢?

#6


3  

Good question. I don't know the specific articles to which you refer, but I have read similar things from time to time. My suspicion is that the writers of such articles tend to harbor a bias against C++-style programming. If the writer programs in C++ only when he must, then returns to Java or such as soon as he can, then he doesn't really share the C++ mindset.

好问题。我不知道你所指的具体文章是什么,但我时常读到类似的东西。我怀疑这类文章的作者倾向于对c++风格的编程抱有偏见。如果作者只在必须的时候才用c++编程,然后尽可能快地返回Java或类似的东西,那么他就没有真正的c++思维。

One suspects that some or most of the same writers prefer garbage-collecting memory managers. I don't, but I think differently than they do.

有人怀疑,同样的一些或大多数作者更喜欢垃圾收集内存管理器。我不知道,但我的想法和他们不一样。

Smart pointers are great, but they have to keep reference counts. The keeping of reference counts bears costs -- often modest costs, but costs nonetheless -- at runtime. There is nothing wrong with saving these costs by using bare pointers, especially if the pointers are managed by destructors.

聪明的指针是很棒的,但是它们必须保持引用计数。保持参考计数的成本——通常是适度的成本,但无论如何——在运行时是要付出代价的。通过使用裸指针来节省这些成本没有错,特别是如果指针是由析构函数管理的。

One of the excellent things about C++ is its support for embedded-systems programming. The use of bare pointers is part of that.

c++的优秀之处在于它对嵌入式系统编程的支持。使用裸指针是其中的一部分。

Update: A commenter has correctly observed that C++'s new unique_ptr (available since TR1) does not count references. The commenter also has a different definition of "smart pointer" than I have in mind. He may be right about the definition.

更新:一个评论者正确地观察到c++的新unique_ptr(从TR1开始可用)不计算引用。评论者对“智能指针”的定义也与我所想的不同。他的定义可能是对的。

Further update: The comment thread below is illuminating. All of it is recommended reading.

进一步更新:下面的注释线程很有启发性。所有这些都是推荐阅读。

#7


2  

There are also other types of smart pointers. You might want a specialized smart pointer for something like network replication (one that detects if it's accessed and sends any modifications to the server or some such), keeps a history of changes, marks the fact that it was accessed so it can be investigated when you save data to disk and so on. Not sure if doing that in the pointer is the best solution but using the built in smart pointer types in libraries could result in people being locked into them and loosing the flexibility.

还有其他类型的智能指针。你可能想要一个专门的智能指针类似网络复制(检测如果访问和向服务器发送任何修改或一些),不断变化的历史,标志着它,因此它可以访问调查当您保存数据到磁盘等等。不确定在指针中这样做是否是最好的解决方案,但是在库中使用内置的智能指针类型可能会导致人们被锁在指针中并失去灵活性。

People can have all kinds of different memory management requirements and solutions beyond smart pointers. I might want to manage memory myself, I could be allocating space for things in a memory pool so it's allocated in advance and not at runtime (useful for games). I might be using a garbage collected implementation of C++ (C++11 makes this possible although none exist yet). Or maybe I'm just not doing anything advanced enough to worry about bothering with them, I can know that I'm not going to forget to uninitialized objects and so on. Maybe I'm just confident in my ability to manage memory without the pointer crutch.

除了智能指针之外,人们还可以有各种不同的内存管理需求和解决方案。我可能想自己管理内存,我可以为内存池中的东西分配空间,所以它是预先分配的,而不是在运行时(对游戏有用)。我可能正在使用c++的垃圾收集实现(c++ 11使这成为可能,尽管还不存在)。或者我只是没有做足够高级的事情来担心它们,我知道我不会忘记未初始化的对象等等。也许我只是对自己在没有指针拐杖的情况下管理内存的能力有信心。

Integration with C is another issue too.

与C集成也是另一个问题。

Another issue is smart pointers are part of the STL. C++ is designed to be usable without the STL.

另一个问题是智能指针是STL的一部分。c++被设计成可以在没有STL的情况下使用。

#8


-1  

It also depends on what domain you work in. I write game engines for a living, we avoid boost like the plague, in games the overhead of boost isn't acceptable. In our core engine we ended up writing our own version of stl (Much like the ea stl).

这也取决于你在哪个领域工作。我以编写游戏引擎为生,我们像瘟疫一样避免升级,在游戏中,升级的开销是不可接受的。在我们的核心引擎中,我们最终编写了自己的stl版本(很像ea stl)。

If i was to write a forms application, i might consider using smart pointers; but once memory management is second nature not having granular control over memory becomes quiet annoying.

如果我要编写一个表单应用程序,我可能会考虑使用智能指针;但是一旦内存管理成为第二天性,对内存没有粒度控制就会变得非常烦人。