Dee Dee - 17 days ago 8
C++ Question

When to use Q_NULLPTR?

I see

Q_NULLPTR
being used liberally in Qt source code and examples, but I have found no documentation for what it is exactly and when it should be used.

For example in this official demonstration of the new Qt SerialBus module added in the new Qt v5.6:

if (!m_canDevice->connectDevice()) {
delete m_canDevice;
m_canDevice = Q_NULLPTR;


Did this serve the purpose of
nullptr
prior to that being added in C++11? If so, now that we have C++11, should I be using
Q_NULLPTR
?

PS: I tried searching the Qt source code for the definition of the macro but failed to find it.

Answer

Did this serve the purpose of nullptr prior to that being added in C++11? If so, now that we have C++11, should I be using Q_NULLPTR?

Yes (somewhat) and No respectively.

C++ was quite lacking back in the days, so Qt had its own stuff, which later became obsolete as C++ caught up on the features.

That being said, Q_NULLPTR is (was) not functionally the same as nullptr, (as Andrei noted, if C++11 is supported it expands to nullptr) it didn't give you the type safety, just syntax "sugar". It illustrated the intent to the person reading the code, not to the compiler as nullptr does.

Comments