我应该尽可能多地使用#ifndef Q_MOC_RUN 吗?

Should I use #ifndef Q_MOC_RUN as much a possible?

我有一个使用 Boost 库的 Qt/C++ 项目,我看到 Boost headers 是这样包含的:

#ifndef Q_MOC_RUN
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#endif

我了解到,如果您不这样做,MOC 可能会导致问题。

问题是,我难道不应该使用这个守卫来包含所有其他绝对不包含 Q_OBJECT marco 的 headers 吗?例如标准库 headers 和其他 non-Qt 库? MOC预处理器运行时不是可以节省很多时间吗?

来自主题Qt5 moc error in combination with boost

First, this is a known MOC issue. The MOC can't expand some of the macros used in the Boost library. I believe the reason Qt 4.8 works is that a workaround for specific Boost macro definitions was added to the MOC configuration for that version.

What you need to do to work around this issue: As stated above, use Q_MOC_RUN to comment out problematic headers. You ONLY need to use Q_MOC_RUN in files that produce a moc file (e.g. myheader.h will produce moc_myheader.cpp). You can limit the hack to just those files. So you don't need to #ifndef all usages of Boost headers in your project which limits the pain of implementing this solution quite a bit.

看来这个问题很久以前就解决了,所以如果你没有任何问题并且不需要支持旧版本的Qt,你可能不会在你以后的代码中添加这个宏。