qt5base-lts/src
Marc Mutz 8ea27bb1c6 Make Q_ASSERT() usable in constexpr functions
We need Q_ASSERT in (C++11) constexpr functions, and the only way to
inject them in C++11 is to use the comma operator. E.g. in
QLatin1String:

    constexpr QLatin1Char at(int i) const
    { return assert(1 >= 0), assert(i < size()), m_data[i]; }

The main problem with our existing Q_ASSERT is that while it is a
ternary expression in active mode, it was a statement in passive
mode. This is easily fixed by dropping the do-while loop and leaving
just its parenthesized exit condition. Add a cast to void, too,
ensuring that Q_ASSERT has type void in both passive and active modes.

But even in C++14 constexpr functions, which accept several
statements, Q_ASSERT needs to have a path through its conditionals
that is constexpr, but neither qt_assert(_x) nor qt_noop() are
constexpr. Nor can they be in C++11 (no void returns in C++11
constexpr functions). I fixed this by replacing qt_noop() with
static_cast<void>(0). The void cast is required so both 2nd and 3rd
arguments to the ternary are void (mixing void and non-void branches
in the ternary is only allowed if the void leg is a
throw-expression[1]).

As a drive-by, adjust to style guide, remove overparenthesization and
reverse the conditional in the ternary.

Apply it to QLatin1String where we had the problem that constexpr
functions had a narrow constract.

[1] should probably be extended to any [[noreturn]] void function,
e.g. std::terminate().

[ChangeLog][QtCore][QtGlobal] Q_ASSERT() and Q_ASSERT_X() now always
expand to expressions of type void that are usable in constexpr
contexts. This makes them usable in both C++11 and C++14 constexpr
functions.

Change-Id: I09c396bc0034ac344cfaadc6f8cbeb1b7b0cbabc
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2017-03-16 14:11:16 +00:00
..
3rdparty pcre2: Replace LGPL21 with LGPL license header 2017-02-28 15:34:20 +00:00
android Merge remote-tracking branch 'origin/5.9' into dev 2017-03-02 09:04:38 +01:00
angle Merge remote-tracking branch 'origin/5.8' into dev 2017-01-25 20:06:06 +01:00
concurrent Doc: Replace std::bind() with lambdas in Qt Concurrent 2017-03-07 04:35:23 +00:00
corelib Make Q_ASSERT() usable in constexpr functions 2017-03-16 14:11:16 +00:00
dbus Remove unused QDBusConnectionPrivate::checkReplyForDelivery() 2017-03-01 21:07:14 +00:00
gui Make QWindow's windowState a QFlags of the WindowState 2017-03-16 12:55:19 +00:00
network Merge remote-tracking branch 'origin/5.9' into dev 2017-03-14 10:52:24 +01:00
opengl Use static initialization for QBasicAtomics 2017-02-16 19:26:46 +00:00
openglextensions Also release winmain, qopenglextensions under commercial licenses 2016-11-18 16:19:56 +00:00
platformheaders XCB: Replace LGPL21 with LGPL license header 2017-02-28 15:34:58 +00:00
platformsupport Make QWindow's windowState a QFlags of the WindowState 2017-03-16 12:55:19 +00:00
plugins Make QWindow's windowState a QFlags of the WindowState 2017-03-16 12:55:19 +00:00
printsupport Add feature.checkbox 2017-03-06 20:34:58 +00:00
sql Merge remote-tracking branch 'origin/5.9' into dev 2017-03-14 10:52:24 +01:00
testlib Testlib: Replace LGPL21 with LGPL license header 2017-03-03 07:26:31 +00:00
tools moc: Add support for C++17 nested namespaces (N4230) 2017-03-08 17:34:38 +00:00
widgets Make QWindow's windowState a QFlags of the WindowState 2017-03-16 12:55:19 +00:00
winmain Merge remote-tracking branch 'origin/5.8' into 5.9 2017-02-08 15:49:18 +01:00
xml QtXml: add some missing Q_DECLARE_TYPEINFO 2017-03-07 01:55:44 +00:00
src.pro New qfloat16 class 2017-01-31 14:21:42 +00:00