C++ 你对 shared_ptr 进行 typedef 的约定是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2717436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's your convention for typedef'ing shared_ptr?
提问by Emile Cormier
I'm flip-flopping between naming conventions for typedef'ing the boost::shared_ptr template. For example:
我在boost::shared_ptr 模板的typedef 命名约定之间来回切换。例如:
typedef boost::shared_ptr<Foo> FooPtr;
Before settling on a convention, I'd like to see what others use. What is your convention?
在确定约定之前,我想看看其他人使用什么。你的约定是什么?
EDIT:
编辑:
To those nesting the typedef inside Foo, doesn't it bother you that Foo is now "aware" of how it will be passed around? It seems to break encapsulation. How about this:
对于那些将 typedef 嵌套在 Foo 中的人来说,Foo 现在“知道”它将如何传递,难道您不会感到困扰吗?它似乎打破了封装。这个怎么样:
class Foo
{
public:
typedef std::vector<Foo> Vector
};
You wouldn't do this now, would you? :-)
你现在不会这样做,是吗?:-)
回答by Lena Schimmel
I'd like too add some options to this old question, even though they might be highly controversial…
我也想为这个老问题添加一些选项,即使它们可能会引起很大争议......
Similar to OldPeculier's answerI like short type names that resemble standard pointers as closely as possible.
与OldPeculier 的回答类似,我喜欢尽可能接近标准指针的短类型名称。
In a project that used shared_pointer
almost everywhere, I used
在一个shared_pointer
几乎无处不在的项目中,我使用了
typedef boost::shared_ptr<Foo> Foo_;
// usage examples:
Foo* myFoo0;
Foo_ myFoo1;
I took advantage of three things:
我利用了三件事:
- That the underscore character somehow looks like an operator, yet is treated mostly like a letter, so that it can be part of an identifier (and I see no rule forbiddingit at the end of the identifier).
- That I only needed to come up with onetypedef.
- I prefer
Foo* myFoo1;
overFoo *myFoo1;
for several reasons, and it matches nicely withFoo_ myFoo2
.
- 下划线字符在某种程度上看起来像一个运算符,但主要被视为一个字母,因此它可以成为标识符的一部分(并且我没有看到禁止在标识符末尾使用它的规则)。
- 我只需要想出一个typedef。
- 我更喜欢
Foo* myFoo1;
有Foo *myFoo1;
几个原因,它与Foo_ myFoo2
.
When in need of typedefs for different kinds of smart pointers, I'd go for
当需要不同类型的智能指针的 typedef 时,我会去
typedef shared_ptr<Foo> Foo_S;
typedef weak_ptr<Foo> Foo_W;
typedef unique_ptr<Foo> Foo_U;
// usage examples:
Foo* myFoo2;
Foo_S myFoo3;
Foo_W myFoo4;
Foo_U myFoo5;
With increasing Unicode support in the standards and compiler implementations, I'd be tempted to try the following syntax, assuming that those star characters would be treated as a regular part of the type identifier. Of course this is only practical if all involved developers have a convenient text input method for this:
随着标准和编译器实现中对 Unicode 支持的增加,我很想尝试以下语法,假设这些星号将被视为类型标识符的常规部分。当然,这只有在所有相关开发人员都有一个方便的文本输入方法时才实用:
typedef shared_ptr<Foo> Foo★;
typedef weak_ptr<Foo> Foo☆;
typedef unique_ptr<Foo> Foo?;
// usage examples:
Foo* myFoo6;
Foo★ myFoo7;
Foo☆ myFoo8;
Foo? myFoo9;
(A quick test indicated that this does not actually work, at least with my build environment. But the same is true for Foo_?
.)
(快速测试表明这实际上不起作用,至少在我的构建环境中是这样。但对于Foo_?
.也是如此。)
回答by pdusen
Answer: don't do it. It's convenient for you and nobody else. Say what you mean.
回答:不要这样做。这对你和其他人来说都很方便。说出你的意思。
回答by James McNellis
My preference:
我的偏好:
class Foo
{
public:
typedef boost::shared_ptr<Foo> SharedPointer;
};
The problem with just FooPtr
is that you may have different types of pointers (e.g., weak_ptr
s). I also don't much care for abbreviations, but that's another matter altogether.
just 的问题FooPtr
在于您可能有不同类型的指针(例如,weak_ptr
s)。我也不太关心缩写,但这完全是另一回事。
回答by Nathan Ernst
Personally, in the code I'm responsible for, you'd typically see a FooPtr typedef'd at the same namespace scope as Foo andFoo would contain a generically named 'SmartPtr' typedef to the same type as FooPtr. Having FooPtr allows for easy an non-verbose manual usage. having the nested typedef for 'SmartPtr' or some quivalent allows for easy generic usage in templates, macros, etc. without having to know that actual type of the smart pointer.
就我个人而言,在我负责的代码中,您通常会在与Foo相同的命名空间范围内看到 FooPtr typedef,并且Foo 将包含一个与 FooPtr 类型相同的通用命名的“SmartPtr” typedef。使用 FooPtr 可以轻松进行非详细的手动使用。为 'SmartPtr' 或一些等价物使用嵌套的 typedef 允许在模板、宏等中轻松通用,而不必知道智能指针的实际类型。
Also, I'd suggest adding a 'subjective' tag to this question.
另外,我建议为这个问题添加一个“主观”标签。
回答by peterchen
I have used both the outer and encapsulated typedef, but ended up with the first,
我使用了外部和封装的 typedef,但最终使用了第一个,
typedef boost::shared_ptr<Foo> FooPtr;
solely because in combined expressions this looks cleaner than Foo::Ptr
.
仅仅因为在组合表达式中这看起来比Foo::Ptr
.
Doesn't it bother you that Foo is now "aware" of how it will be passed around?
Foo 现在“知道”它将如何传递,这不打扰您吗?
Often enough, these classes are creatable through a factory method only:
通常,这些类只能通过工厂方法创建:
struct Foo
{
static FooPtr Create() { return FooPtr(new Foo); }
protected:
Foo() {}
}
That's kind of "stronger" than encapsulating the typedef, yet a very common pattern.
这比封装 typedef 更“强大”,但这是一种非常常见的模式。
回答by Nate
I'm not a big fan of Hungarian naming conventions, I usually use:
我不是匈牙利命名约定的忠实粉丝,我通常使用:
typedef boost::shared_ptr<Foo> FooSharedPtr;
Detailed enough to be clear but short enough to not be a huge hassle. In any case, I would definitely indicate it's specifically a shared pointer, especially if you're not the only one who's going to be using that type in the future.
足够详细,清晰,但足够简短,不会造成太大的麻烦。无论如何,我肯定会指出它是一个共享指针,特别是如果您不是将来唯一要使用该类型的人。
回答by OldPeculier
I'm generally not a fan of very short identifiers, but this is one case where I'll use them.
我通常不喜欢非常短的标识符,但这是我将使用它们的一种情况。
class Foo
{
public:
typedef std::shared_ptr<Foo> p;
};
This enables shared_ptr to resemble ordinary pointers as closely as possible without risk of confusion.
这使得 shared_ptr 尽可能地类似于普通指针,而不会产生混淆的风险。
Foo* myFoo;
Foo::p myFoo;
And as for breaking encapsulation—no, typedefing the shared_ptr type within the class doesn't break encapsulation any more than typedefing it outside of the class. What meaning of "encapsulation" would it violate? You're not revealing anything about the implementation of Foo. You're just defining a type. This is perfectly analogous to the relationship between Foo* and Foo. Foo* is a certain kind of pointer to Foo (the default kind, as it happens). Foo::p is another kind of pointer to Foo. You're not breaking encapsulation, you're just adding to the type system.
至于破坏封装——不,在类内对 shared_ptr 类型进行类型定义不会破坏封装,就像在类外对其进行类型定义一样。它会违反“封装”的什么含义?你没有透露任何关于 Foo 的实现。你只是在定义一个类型。这完全类似于 Foo* 和 Foo 之间的关系。Foo* 是某种指向 Foo 的指针(默认类型,碰巧)。Foo::p 是另一种指向 Foo 的指针。你没有破坏封装,你只是添加到类型系统。
回答by KeithB
I usually encapsulate the the typedef
inside the class. The reason is that we have some memory sensitive code, and it makes it easy to switch between boost::shared_ptr
and boost::intrusive_ptr
Since intrusive_ptr
is something that the class needs to support, it makes sense to me to have the knowledge of which shared pointer to use be wrapped up in the class.
我通常封装typedef
类内部。原因是我们有一些内存敏感的代码,它可以很容易地在boost::shared_ptr
和boost::intrusive_ptr
因为intrusive_ptr
是类需要支持的东西,所以知道使用哪个共享指针的知识被包裹在班级。
回答by user846566
typedef shared_ptr<Foo> sptr_Foo;
typedef weak_ptr<Foo> wptr_Foo;
typedef unique_ptr<Foo> uptr_Foo;
回答by Edward Strange
My first response is to ask, "Why typedef that?"
我的第一反应是问,“为什么要 typedef 呢?”
In reply to your edit: Actually that's a rather interesting approach that could be useful in many situations. Using it to go back to your original question you might have:
回复您的编辑:实际上,这是一种相当有趣的方法,在许多情况下都可能有用。使用它来回到您可能有的原始问题:
struct object
{
typedef object* ptr_t;
typedef shared_ptr<object> shared_ptr_t;
typedef weak_ptr<object> weak_ptr_t;
typedef unique_ptr<object> unique_ptr_t;
etc...
}