Q_ARRAY_LITERAL: protect the check for literal types

Some compilers (hello, MSVC) do not produce literal types in Qt
because their constexpr support has been blacklisted.
Therefore, amend the check for literal types in Q_ARRAY_LITERAL:
only do the check if the compiler supports constexpr.

Change-Id: I7cffe00dde447d975aa6a7d02248df9c351508ff
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Giuseppe D'Angelo 2019-07-01 20:55:46 +02:00 committed by Marc Mutz
parent eea99e1e8f
commit 3a1f9dec7c
2 changed files with 19 additions and 1 deletions

View File

@ -323,8 +323,14 @@ struct QArrayDataPointerRef
}()) \
/**/
#ifdef Q_COMPILER_CONSTEXPR
#define Q_ARRAY_LITERAL_CHECK_LITERAL_TYPE(Type) Q_STATIC_ASSERT(std::is_literal_type<Type>::value)
#else
#define Q_ARRAY_LITERAL_CHECK_LITERAL_TYPE(Type) do {} while (0)
#endif
#define Q_ARRAY_LITERAL_IMPL(Type, ...) \
Q_STATIC_ASSERT(std::is_literal_type<Type>::value); \
Q_ARRAY_LITERAL_CHECK_LITERAL_TYPE(Type); \
\
/* Portable compile-time array size computation */ \
Q_CONSTEXPR Type data[] = { __VA_ARGS__ }; Q_UNUSED(data); \

View File

@ -1625,6 +1625,18 @@ void tst_QArrayData::literals()
QCOMPARE(const_(v)[i], char('A' + i));
QCOMPARE(const_(v)[10], char('\0'));
}
{
struct LiteralType {
int value;
Q_DECL_CONSTEXPR LiteralType(int v = 0) : value(v) {}
};
QArrayDataPointer<LiteralType> d = Q_ARRAY_LITERAL(LiteralType, LiteralType(0), LiteralType(1), LiteralType(2));
QCOMPARE(d->size, 3);
for (int i = 0; i < 3; ++i)
QCOMPARE(d->data()[i].value, i);
}
}
// Variadic Q_ARRAY_LITERAL need to be available in the current configuration.