您对 shared_ptr 进行 typedef 的约定是什么?
我正在为 boost::shared_ptr
模板转换 typedef
的命名约定.例如:
I'm flip-flopping between naming conventions for typedef
'ing the boost::shared_ptr
template. For example:
typedef boost::shared_ptr<Foo> FooPtr;
在确定约定之前,我想看看其他人使用什么.你的约定是什么?
Before settling on a convention, I'd like to see what others use. What is your convention?
对于那些将 typedef
嵌套在 Foo
中的人来说,Foo
现在知道"了,难道你不会感到困扰吗?它将如何传递?它似乎打破了封装.这个怎么样:
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:
class Foo
{
public:
typedef std::vector<Foo> Vector;
};
你现在不会这样做,对吗?:-)
You wouldn't do this now, would you? :-)
推荐答案
我也想为这个老问题添加一些选项,即使它们可能引起高度争议......
类似于 OldPeculier 的回答我喜欢尽可能接近标准指针的短类型名称.
Similar to OldPeculier's answer I like short type names that resemble standard pointers as closely as possible.
在一个几乎到处都使用 shared_pointer
的项目中,我使用了
In a project that used shared_pointer
almost everywhere, I used
typedef boost::shared_ptr<Foo> Foo_;
// usage examples:
Foo* myFoo0;
Foo_ myFoo1;
我利用了三件事:
- 下划线字符不知何故看起来像一个运算符,但主要被视为一个字母,因此它可以成为标识符的一部分(我看到 没有规则禁止在标识符的末尾).
- 我只需要想出一个 typedef.
- 出于多种原因,我更喜欢
Foo* myFoo1;
而不是Foo *myFoo1;
,并且它与Foo_ myFoo2
非常匹配.
- 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 forbidding it at the end of the identifier).
- That I only needed to come up with one typedef.
- I prefer
Foo* myFoo1;
overFoo *myFoo1;
for several reasons, and it matches nicely withFoo_ myFoo2
.
当需要针对不同类型智能指针的 typedef 时,我会去
When in need of typedefs for different kinds of smart pointers, I'd go for
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;
随着标准和编译器实现中对 Unicode 支持的增加,我很想尝试以下语法,假设这些星号字符将被视为类型标识符的常规部分.当然,这只有在所有相关开发人员都有一个方便的文本输入方法时才实用:
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:
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;
(快速测试表明这实际上不起作用,至少在我的构建环境中是这样.但对于 Foo_?
也是如此.)
(A quick test indicated that this does not actually work, at least with my build environment. But the same is true for Foo_?
.)
相关文章