什么是对typedef'ing共享指针的最佳策略是什么? [英] What's the best strategy for typedef'ing shared pointers?

查看:141
本文介绍了什么是对typedef'ing共享指针的最佳策略是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于使用的typedef的冗长的模板,一个快速的问题。症结:我发现自己在一个东西泡菜,似乎没有被放置类型定义,除了本地客户端功能的好地方。虽然也有类似的 SO 的问题(见<一href=\"http://stackoverflow.com/questions/2717436/whats-your-convention-for-typedefing-shared-ptr\">here例如),无似乎正好解决这个问题。请注意,这个问题没有解决的typedef不管是在什么可取如下 - 我试图把事情简单化为了说明整个过程。

I have a quick question regarding the use of typedefs for lengthy templates. The crux: I've found myself in something of a pickle—there doesn't seem to be a good place to place typedefs except local to client functions. While there are similar SO questions (see here for example), none seem to address this exactly. Please note that this question doesn't address whether typedefs are desirable in what follows—I've tried to simplify things for expository purposes.

升压工作时,我的问题已经出现:: shared_ptr的&LT; T&GT; 。基本上,我要做到以下几点:

My problem has arisen while working with boost::shared_ptr<T>. Basically, I want to do the following:

#include <boost/shared_ptr.hpp>
typedef boost::shared_ptr<Widget> WidgetPtr;

配售本的typedef在小部件声明头显得难看。似乎有两个方面的考虑在这里:(i)如小部件本身不利用其成员共同指针,我们添加一个附加的包含(因为我们可以'T转发声明的boost :: shared_ptr的模板类,纠正我,如果我错了?)(二)如果我们想的声明中利用这一的typedef另一个类(调用类)我们违反最佳做法包括 Widget.h 而不是简单地向前声明小部件或包含 WidgetFwd.h ...除非这类型定义是在后者的复制。此外,它似乎有道理不给的typedef 的boost :: shared_ptr的&LT;窗​​口小部件&GT; 控件的声明中本身 - 我们似乎混合小部件的宣言与客户将如何利用小部件接口的期待。

Placing this typedef in the Widget declaration header seems ugly. There seem to be two considerations here: (i) if Widget itself doesn't make use of shared pointers in its members, we've added an additional include (as we can't forward declare the boost::shared_ptr template class—correct me if I'm wrong?) (ii) if we want to make use of this typedef during the declaration of another class (call that class Foo) we violate best practices by including Widget.h instead of simply forward declaring Widget or including WidgetFwd.h... unless this typedef is duplicated in the latter. Furthermore, it doesn't seem make sense to typedef boost::shared_ptr<Widget> during the declaration of Widget itself—we seem to be mixing Widget's declaration with an anticipation of how clients will make use of the Widget interface.

好了,所以这是很糟糕,但是这更糟糕的是:如果我不尝试上面我结束了在客户端code重复的类型定义的某种组合,这将产生不一致(因此,可能的是,错误) - 被给定整点小部件,一个 WidgetPtr 的typedef应充当其本身的类型。例如:我们不希望来利用一个 WidgetPtr 的一个typedef提高:: shared_ptr的,而酒吧使用WidgetPtr为的std :: auto_ptr的。

Okay, so that's bad, but this is worse: if I don't attempt some combination of the above I end up with duplicate typedefs in client code, which yields inconsistency (and hence, likely, error)—the whole point being that given Widget, a WidgetPtr typedef should act as a type in its own right. Example: we don't want Foo to make use of one WidgetPtr, a typedef of boost::shared_ptr, while Bar is using WidgetPtr as a typedef for std::auto_ptr.

另一种方法(和我已经看到了网上的讨论中提到的几个之一)将是使typedef的小部件的公开成员,然后使用的Widget :: PTR

Another method (and one of the few that I've seen mentioned in online discussion) would be to make the typedef a public member of Widget and then use Widget::Ptr:

class Widget {
// ...
public:
     typedef boost::shared_ptr<Widget> Ptr;
};

同样,我不喜欢这个为(i)其表明指针类型是某种类的成员及(ii)其导致靠不住的接口。更糟糕的是:因为每次我写的类可能会被指向使用智能指针,我结束了追逐虚构客户的尾巴。丑陋的,丑陋的,丑陋的。

Again, I don't like this as (i) it suggests that the pointer type is somehow a member of the class and (ii) it leads to a wonky interface. Worse still: since every class that I write can potentially be pointed to using smart pointers, I end up chasing the imaginary client's tail. Ugly, ugly, ugly.

既然这样,我已经移除此codeBase包括类型定义(因为它们导致了严重的混乱,重复数据删除),并在选定的功能重新引入在本地。这里同样有不一致的使用问题,但它不是很严重。

As it stands, I've removed the typedefs from this codebase (as they led to serious confusion, duplication) and re-introduced them locally in selected functions. Here again there's a problem with inconsistent usage but it's not quite as severe.

唯一的其他解决方案,我能想到的,我又我不知道这是否是好的做法,将有一个实用工具头,其中的typedef放置,有可能在自己的命名空间。在这头,我们会包括与它来完成。

The only other solution I can think of—and again I'm not sure whether this is considered good practice—would be to have a utilities header in which the typedefs are placed, potentially within their own namespace. In this header we'd include and be done with it.

我失去了一些东西明显,或者这只是普通的猫腻?

Am I missing something obvious or is this just plain tricky?

PS-道歉以上的长度;我找不到充分的前$ P $的pssing问题的简单的方法。

PS—Apologies for the length of the above; I couldn't find a simpler way of fully expressing the problem.

推荐答案

我不喜欢一个库支配使用特定的智能指针,但我忍受它,如果它是必要的。

I don't like a library dictating the use of a particular smart pointer, but I tolerate it if it is necessary.

如果你想强制用户始终使用的shared_ptr 来操纵一个小部件,这是不可能的,所以甚至不打扰尝试。

If you wish to force users to always use a shared_ptr to manipulate a widget, it's impossible, so don't even bother trying.

在另一方面,如果你从小部件方法返回一个的boost :: shared_ptr的&LT;窗​​口小部件&GT; ,然后提供一个(理智)的typedef可能简化客户端code。

On the other hand, if you have a method from Widget which returns a boost::shared_ptr<Widget>, then providing a (sane) typedef might simplify the client code.

因此​​,我要推广使用内部的typedef:

I would therefore promote the use of an inner typedef:

class Widget
{
public:
  typedef boost::shared_ptr<Widget> Ptr;

  Ptr AccessFirstChild();
}; // class Widget

在这种情况下,这是完全没关系的#include 必要的标头。

in which case it's perfectly okay to #include the necessary headers.

这篇关于什么是对typedef'ing共享指针的最佳策略是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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