[Qt-creator] Minimizing compile-time conditionals in Qt Creator

Coda Highland chighland at gmail.com
Tue Aug 28 18:59:46 CEST 2012


On Tue, Aug 28, 2012 at 9:52 AM, Stephen Chu <stephen at ju-ju.com> wrote:
> On 8/28/12 8:50 AM, Christian Kandeler wrote:
>> On 28/08/12 14:19, ext Stephen Chu wrote:
>>> On 8/28/12 3:56 AM, Christian Kandeler wrote:
>>>>        QStringList rc(QLatin1String(".svn"));
>>>>        if (Utils::HostOsInfo::isWindowsHost())
>>>>            rc.push_back(QLatin1String("_svn"));
>>>>        return rc;
>>>
>>> Doesn't this generate dead code that will never be executed on platforms
>>> other than the intended one?
>>
>> Yes (with C++03 at least). And if you can provide an example where it
>> demonstrably matters, we can switch back to ifdefs for that particular
>> piece of code.
>
> It's not just dead code. It's also the calling of the conditional
> function and the testing of it. These are all runtime cost that can and
> should be avoided.
>
> Something like this in some frequently called function (string?) can
> impact the performance negatively.

The point has been made that the compiler can do a lot of optimization
on this. If the header file contains:

inline bool isWindowsHost() const {
#if Q_OS_WIN
    return true;
#else
    return false;
#endif
}

then any compiler worth its salt will remove the conditional at
compile-time by inlining the function into the conditional and then
observing that if(true) or if(false) is constant and thereby remove
both the check and the dead code.

/s/ Adam



More information about the Qt-creator mailing list