My application may be used on systems that are still defaulting to Qt4.x rather than Qt5.x and I was trying to do a "quick and dirty" port of a version that would compile/link and run with either major version of the libraries. I was wondering what would be the best way to get around the absence of Q_NULLPTR in Qt4.x? I saw the link mentioned in this SO answer which indicated the definition as:
我的应用程序可以在仍然默认为Qt4的系统上使用。而不是Qt5 x。我和x试图做一个“快速而肮脏”的版本端口,该版本将编译/链接并运行于库的任何一个主要版本。我想知道在Qt4.x中绕过Q_NULLPTR的最好方法是什么?我看到了这个答案中提到的联系它表明了定义是:
#ifdef Q_COMPILER_NULLPTR
# define Q_NULLPTR nullptr
#else
# define Q_NULLPTR NULL
#endif
So should I just use a:
我应该用a:
contains(QT_MAJOR_VERSION, 4) {
DEFINES += Q_NULLPTR=NULL
}
in my qmake
project file?
在我的qmake项目文件中?
1 个解决方案
#1
0
Q_NULLPTR
is Qt’s way to support both C++11 capable compilers as well as older C++98/03 ones. Do you actually need that level of sophistication? If not, then just use NULL
, 0
or nullptr
directly, depending on your compiler.
Q_NULLPTR是Qt的方法,它既支持c++ 11的编译器,也支持旧的c++ 98/03的编译器。你真的需要那种复杂的程度吗?如果不是,那么直接使用NULL, 0或nullptr,这取决于您的编译器。
Otherwise things get more complicated because you need to detect the currently active C++ version. See edit below.
否则事情会变得更复杂,因为您需要检测当前活动的c++版本。请参阅下面的编辑。
Of course you can always choose the easy way and use NULL
or 0
which works in all versions of C++.
当然,您可以选择简单的方法并使用NULL或0,这在c++的所有版本中都适用。
Edit
编辑
To mimic Qt5’s behaviour you could use this:
要模仿Qt5的行为,您可以使用以下方法:
#if __cplusplus >= 201103L
#define Q_NULLPTR nullptr
#else
// not NULL to stay consistent with Qt’s convention
#define Q_NULLPTR 0
#endif
Including a header just to have access to Q_NULLPTR
isn’t terribly great, so you’ll probably want to define that macro in a .pro file. However, I don’t know how to detect the active C++ version with Qt4’s QMake.
包括一个仅能访问Q_NULLPTR的头并不太好,因此您可能需要在.pro文件中定义该宏。但是,我不知道如何使用Qt4的QMake检测活动c++版本。
#1
0
Q_NULLPTR
is Qt’s way to support both C++11 capable compilers as well as older C++98/03 ones. Do you actually need that level of sophistication? If not, then just use NULL
, 0
or nullptr
directly, depending on your compiler.
Q_NULLPTR是Qt的方法,它既支持c++ 11的编译器,也支持旧的c++ 98/03的编译器。你真的需要那种复杂的程度吗?如果不是,那么直接使用NULL, 0或nullptr,这取决于您的编译器。
Otherwise things get more complicated because you need to detect the currently active C++ version. See edit below.
否则事情会变得更复杂,因为您需要检测当前活动的c++版本。请参阅下面的编辑。
Of course you can always choose the easy way and use NULL
or 0
which works in all versions of C++.
当然,您可以选择简单的方法并使用NULL或0,这在c++的所有版本中都适用。
Edit
编辑
To mimic Qt5’s behaviour you could use this:
要模仿Qt5的行为,您可以使用以下方法:
#if __cplusplus >= 201103L
#define Q_NULLPTR nullptr
#else
// not NULL to stay consistent with Qt’s convention
#define Q_NULLPTR 0
#endif
Including a header just to have access to Q_NULLPTR
isn’t terribly great, so you’ll probably want to define that macro in a .pro file. However, I don’t know how to detect the active C++ version with Qt4’s QMake.
包括一个仅能访问Q_NULLPTR的头并不太好,因此您可能需要在.pro文件中定义该宏。但是,我不知道如何使用Qt4的QMake检测活动c++版本。