typedef的头文件最佳实践 [英] Header file best practices for typedefs

查看:106
本文介绍了typedef的头文件最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中广泛使用shared_ptr和STL,这导致过长,容易出错的类型,例如 shared_ptr<载体, shared_ptr< const Foo> > > (我是一个ObjC程序员,偏好,长名称是规范,仍然这是太多了。)我会相信,更清楚,一贯称为 FooListPtr 并记录Ptr意味着shared_ptr和List意味着vector_ptr的向量的命名约定。

I'm using shared_ptr and STL extensively in a project, and this is leading to over-long, error-prone types like shared_ptr< vector< shared_ptr<const Foo> > > (I'm an ObjC programmer by preference, where long names are the norm, and still this is way too much.) It would be much clearer, I believe, to consistently call this FooListPtr and documenting the naming convention that "Ptr" means shared_ptr and "List" means vector of shared_ptr.

到typedef,但它导致头痛的标题。我似乎有几个选项在哪里定义 FooListPtr

This is easy to typedef, but it's causing headaches with the headers. I seem to have several options of where to define FooListPtr:


  • Foo.h 。这会缠绕所有的标题并产生严重的构建问题,因此它是一个非初始的。

  • FooFwd.h(forward header)。这是基于iosfwd.h的 Effective C ++ 建议。这是非常一致的,但维护两倍的标头数量的开销似乎恼人的最好。

  • Common.h(将所有的一起放到一个文件)。这通过缠绕许多不相关的类型来杀死可重用性。你现在不能只拾取一个对象并将其移动到另一个项目。

  • 如果没有typedefed的话,typedef的一些奇怪的定义魔法。我对预处理器持续不喜欢,因为我认为这让新人难以理解代码,但也许.... ....

  • 使用向量子类而不是typedef。这似乎很危险...

  • Foo.h. That entwines all the headers and creates serious build problems, so it's a non-starter.
  • FooFwd.h ("forward header"). This is what Effective C++ suggests, based on iosfwd.h. It's very consistent, but the overhead of maintaining twice the number of headers seems annoying at best.
  • Common.h (put all of them together into one file). This kills reusability by entwining a lot of unrelated types. You now can't just pick up one object and move it to another project. That's a non-starter.
  • Some kind of fancy #define magic that typedef's if it hasn't already been typedefed. I have an abiding dislike for the preprocessor because I think it makes it hard for new people to grok the code, but maybe....
  • Use a vector subclass rather than a typedef. This seems dangerous...

这里有最佳做法吗?在可重用性,可读性和一致性至关重要的情况下,它们如何以实际代码形式出现?

Are there best practices here? How do they turn out in real code, when reusability, readability and consistency are paramount?

如果其他人想添加其他选项进行讨论,我已标记此社区Wiki。

I've marked this community wiki if others want to add additional options for discussion.

推荐答案

我在一个项目上编程,听起来像是使用 common.h 方法。

I'm programming on a project which sounds like it uses the common.h method. It works very well for that project.

有一个名为 ForwardsDecl.h 的文件,编译头并简单地向前声明所有重要的类和必要的typedef。在这种情况下,使用 unique_ptr 而不是 shared_ptr ,但使用应该类似。它看起来像这样:

There is a file called ForwardsDecl.h which is in the pre-compiled header and simply forward-declares all the important classes and necessary typedefs. In this case unique_ptr is used instead of shared_ptr, but the usage should be similar. It looks like this:

// Forward declarations
class ObjectA;
class ObjectB;
class ObjectC;

// List typedefs
typedef std::vector<std::unique_ptr<ObjectA>> ObjectAList;
typedef std::vector<std::unique_ptr<ObjectB>> ObjectBList;
typedef std::vector<std::unique_ptr<ObjectC>> ObjectCList;

Visual C ++ 2010接受此代码,即使类只是向前声明定义是没有必要的,所以没有必要包括每个类的头文件)。我不知道这是标准和其他编译器将需要完整的类定义,但它是有用的,它不:另一个类(ObjectD)可以有一个ObjectAList作为成员,而不需要包括ObjectA.h - 这可以真正帮助减少头文件依赖!

This code is accepted by Visual C++ 2010 even though the classes are only forward-declared (the full class definitions are not necessary so there's no need to include each class' header file). I don't know if that's standard and other compilers will require the full class definition, but it's useful that it doesn't: another class (ObjectD) can have an ObjectAList as a member, without needing to include ObjectA.h - this can really help reduce header file dependencies!

维护并不是一个特别的问题,因为转发声明只需要写一次,任何后续的更改只需要在完整的声明在类的头文件(这将触发更少的源文件被重新编译,由于减少依赖)。

Maintenance is not particularly an issue, because the forwards declarations only need to be written once, and any subsequent changes only need to happen in the full declaration in the class' header file (and this will trigger fewer source files to be recompiled due to reduced dependencies).

最后,它似乎可以在项目之间共享我没有试过自己),因为即使一个项目实际上没有声明一个ObjectA,它没有关系,因为它只是转发声明,如果你不使用它编译器不在乎。因此,该文件可以包含所有使用它的项目的类名,而且如果某个项目缺少某些项目,则无关紧要。所需要的是必要的完整声明头(例如 ObjectA.h )包含在任何(.cpp)实际使用。

Finally it appears this can be shared between projects (I haven't tried myself) because even if a project does not actually declare an ObjectA, it doesn't matter because it was only forwards declared and if you don't use it the compiler doesn't care. Therefore the file can contain the names of classes across all projects it's used in, and it doesn't matter if some are missing for a particular project. All that is required is the necessary full declaration header (e.g. ObjectA.h) is included in any source (.cpp) files that actually use them.

这篇关于typedef的头文件最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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