1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2017 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtCore/QtCore>
#include <QtTest/QtTest>
#include <algorithm>
#define BASECLASS_NOT_ABSTRACT
#include "baseclass.h"
#include "derivedclass.h"
#ifdef Q_COMPILER_ATOMICS
# include <atomic>
#endif
QT_USE_NAMESPACE
class tst_Compiler : public QObject
{
Q_OBJECT
private slots:
/* C++98 & C++03 base functionality */
void template_methods();
void template_constructors();
void template_subclasses();
void methodSpecialization();
void constructorSpecialization();
void staticTemplateMethods();
void staticTemplateMethodSpecialization();
void detectDataStream();
void detectEnums();
void overrideCFunction();
void stdSortQList();
void stdSortQVector();
void templateCallOrder();
void virtualFunctionNoLongerPureVirtual();
void charSignedness() const;
void privateStaticTemplateMember() const;
void staticConstUnionWithInitializerList() const;
void templateFriends();
/* C++11 features */
void cxx11_alignas();
void cxx11_alignof();
void cxx11_alignas_alignof();
void cxx11_atomics();
void cxx11_attributes();
void cxx11_auto_function();
void cxx11_auto_type();
void cxx11_class_enum();
void cxx11_constexpr();
void cxx11_decltype();
void cxx11_default_members();
void cxx11_delete_members();
void cxx11_delegating_constructors();
void cxx11_explicit_conversions();
void cxx11_explicit_overrides();
void cxx11_extern_templates();
void cxx11_inheriting_constructors();
void cxx11_initializer_lists();
void cxx11_lambda();
void cxx11_nonstatic_member_init();
void cxx11_noexcept();
void cxx11_nullptr();
void cxx11_range_for();
void cxx11_raw_strings();
void cxx11_ref_qualifiers();
void cxx11_rvalue_refs();
void cxx11_static_assert();
void cxx11_template_alias();
void cxx11_thread_local();
void cxx11_udl();
void cxx11_unicode_strings();
void cxx11_uniform_init();
void cxx11_unrestricted_unions();
void cxx11_variadic_macros();
void cxx11_variadic_templates();
/* C++14 compiler features */
void cxx14_binary_literals();
void cxx14_init_captures();
void cxx14_generic_lambdas();
void cxx14_constexpr();
void cxx14_decltype_auto();
void cxx14_return_type_deduction();
void cxx14_aggregate_nsdmi();
void cxx14_variable_templates();
/* Future / Technical specification compiler features */
void runtimeArrays();
};
#if defined(Q_CC_HPACC)
# define DONT_TEST_TEMPLATE_CONSTRUCTORS
# define DONT_TEST_CONSTRUCTOR_SPECIALIZATION
# define DONT_TEST_DATASTREAM_DETECTION
#endif
#if defined(Q_CC_SUN)
# define DONT_TEST_STL_SORTING
#endif
class TemplateMethodClass
{
public:
template <class T>
T foo() { return 42; }
};
void tst_Compiler::template_methods()
{
TemplateMethodClass t;
QCOMPARE(t.foo<int>(), 42);
QCOMPARE(t.foo<long>(), 42l);
QCOMPARE(t.foo<double>(), 42.0);
}
#ifndef DONT_TEST_TEMPLATE_CONSTRUCTORS
class TemplateConstructorClass
{
public:
template <class T>
TemplateConstructorClass(const T& t) { i = int(t); }
int i;
};
void tst_Compiler::template_constructors()
{
TemplateConstructorClass t1(42);
TemplateConstructorClass t2(42l);
TemplateConstructorClass t3(42.0);
QCOMPARE(t1.i, 42);
QCOMPARE(t2.i, 42);
QCOMPARE(t3.i, 42);
}
#else
void tst_Compiler::template_constructors()
{ QSKIP("Compiler doesn't do template constructors"); }
#endif
template <typename T>
struct OuterClass
{
template <typename U>
struct InnerClass
{
U convert(const T &t) { return static_cast<U>(t); }
};
};
void tst_Compiler::template_subclasses()
{
OuterClass<char>::InnerClass<int> c1;
QCOMPARE(c1.convert('a'), int('a'));
OuterClass<QRect>::InnerClass<QRectF> c2;
QCOMPARE(c2.convert(QRect(1, 2, 3, 4)), QRectF(QRect(1, 2, 3, 4)));
}
class TemplateMethodClass2
{
public:
template <class T>
T foo() { return 42; }
};
template<>
int TemplateMethodClass2::foo<int>()
{ return 43; }
void tst_Compiler::methodSpecialization()
{
TemplateMethodClass2 t;
QCOMPARE(t.foo<int>(), 43);
QCOMPARE(t.foo<long>(), 42l);
QCOMPARE(t.foo<double>(), 42.0);
}
#ifndef DONT_TEST_CONSTRUCTOR_SPECIALIZATION
class TemplateConstructorClass2
{
public:
template <class T>
TemplateConstructorClass2(const T &t) { i = int(t); }
int i;
};
template<>
TemplateConstructorClass2::TemplateConstructorClass2(const int &t) { i = t + 1; }
void tst_Compiler::constructorSpecialization()
{
TemplateConstructorClass2 t1(42);
TemplateConstructorClass2 t2(42l);
TemplateConstructorClass2 t3(42.0);
QCOMPARE(t1.i, 43);
QCOMPARE(t2.i, 42);
QCOMPARE(t3.i, 42);
}
#else
void tst_Compiler::constructorSpecialization()
{ QSKIP("Compiler doesn't do constructor specialization"); }
#endif
class StaticTemplateClass
{
public:
template <class T>
static T foo() { return 42; }
};
void tst_Compiler::staticTemplateMethods()
{
QCOMPARE(StaticTemplateClass::foo<int>(), 42);
QCOMPARE(StaticTemplateClass::foo<uint>(), 42u);
}
class StaticTemplateClass2
{
public:
template <class T>
static T foo() { return 42; }
};
template<>
double StaticTemplateClass2::foo<double>() { return 18.5; }
void tst_Compiler::staticTemplateMethodSpecialization()
{
QCOMPARE(StaticTemplateClass2::foo<int>(), 42);
QCOMPARE(StaticTemplateClass2::foo<uint>(), 42u);
QCOMPARE(StaticTemplateClass2::foo<double>(), 18.5);
}
#ifndef DONT_TEST_DATASTREAM_DETECTION
/******* DataStream tester *********/
namespace QtTestInternal
{
struct EmptyStruct {};
struct LowPreferenceStruct { LowPreferenceStruct(...); };
EmptyStruct operator<<(QDataStream &, const LowPreferenceStruct &);
EmptyStruct operator>>(QDataStream &, const LowPreferenceStruct &);
template<typename T>
struct DataStreamChecker
{
static EmptyStruct hasStreamHelper(const EmptyStruct &);
static QDataStream hasStreamHelper(const QDataStream &);
static QDataStream &dsDummy();
static T &dummy();
#ifdef BROKEN_COMPILER
static const bool HasDataStream =
sizeof(hasStreamHelper(dsDummy() << dummy())) == sizeof(QDataStream)
&& sizeof(hasStreamHelper(dsDummy() >> dummy())) == sizeof(QDataStream);
#else
enum {
HasOutDataStream = sizeof(hasStreamHelper(dsDummy() >> dummy())) == sizeof(QDataStream),
HasInDataStream = sizeof(hasStreamHelper(dsDummy() << dummy())) == sizeof(QDataStream),
HasDataStream = HasOutDataStream & HasInDataStream
};
#endif
};
template<bool>
struct DataStreamOpHelper
{
template <typename T>
struct Getter {
static QMetaType::SaveOperator saveOp() { return 0; }
};
};
template<>
struct DataStreamOpHelper<true>
{
template <typename T>
struct Getter {
static QMetaType::SaveOperator saveOp()
{
return ::QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Save;
}
};
};
template<typename T>
inline QMetaType::SaveOperator getSaveOperator(T * = 0)
{
typedef typename DataStreamOpHelper<DataStreamChecker<T>::HasDataStream>::template Getter<T> GetterHelper;
return GetterHelper::saveOp();
}
};
struct MyString: public QString {};
struct Qxxx {};
void tst_Compiler::detectDataStream()
{
QVERIFY(QtTestInternal::DataStreamChecker<int>::HasDataStream);
QVERIFY(QtTestInternal::DataStreamChecker<uint>::HasDataStream);
QVERIFY(QtTestInternal::DataStreamChecker<char *>::HasDataStream == true);
QVERIFY(QtTestInternal::DataStreamChecker<const int>::HasInDataStream == true);
QVERIFY(QtTestInternal::DataStreamChecker<const int>::HasOutDataStream == false);
QVERIFY(QtTestInternal::DataStreamChecker<const int>::HasDataStream == false);
QVERIFY(QtTestInternal::DataStreamChecker<double>::HasDataStream);
QVERIFY(QtTestInternal::DataStreamChecker<QString>::HasDataStream);
QVERIFY(QtTestInternal::DataStreamChecker<MyString>::HasDataStream);
QVERIFY(!QtTestInternal::DataStreamChecker<Qxxx>::HasDataStream);
QVERIFY(QtTestInternal::getSaveOperator<int>() != 0);
QVERIFY(QtTestInternal::getSaveOperator<uint>() != 0);
QVERIFY(QtTestInternal::getSaveOperator<char *>() != 0);
QVERIFY(QtTestInternal::getSaveOperator<double>() != 0);
QVERIFY(QtTestInternal::getSaveOperator<QString>() != 0);
QVERIFY(QtTestInternal::getSaveOperator<MyString>() != 0);
QVERIFY(!QtTestInternal::getSaveOperator<Qxxx>());
}
#else
void tst_Compiler::detectDataStream()
{ QSKIP("Compiler doesn't evaluate templates correctly"); }
#endif
enum Enum1 { Foo = 0, Bar = 1 };
enum Enum2 {};
enum Enum3 { Something = 1 };
template <typename T> char QTypeInfoEnumHelper(T);
template <typename T> void *QTypeInfoEnumHelper(...);
template <typename T>
struct QTestTypeInfo
{
enum { IsEnum = sizeof(QTypeInfoEnumHelper<T>(0)) == sizeof(void*) };
};
void tst_Compiler::detectEnums()
{
QVERIFY(QTestTypeInfo<Enum1>::IsEnum);
QVERIFY(QTestTypeInfo<Enum2>::IsEnum);
QVERIFY(QTestTypeInfo<Enum3>::IsEnum);
QVERIFY(!QTestTypeInfo<int>::IsEnum);
QVERIFY(!QTestTypeInfo<char>::IsEnum);
QVERIFY(!QTestTypeInfo<uint>::IsEnum);
QVERIFY(!QTestTypeInfo<short>::IsEnum);
QVERIFY(!QTestTypeInfo<ushort>::IsEnum);
QVERIFY(!QTestTypeInfo<void*>::IsEnum);
QVERIFY(!QTestTypeInfo<QString>::IsEnum);
QVERIFY(QTestTypeInfo<Qt::Key>::IsEnum);
QVERIFY(QTestTypeInfo<Qt::ToolBarArea>::IsEnum);
QVERIFY(!QTestTypeInfo<Qt::ToolBarAreas>::IsEnum);
QVERIFY(QTestTypeInfo<Qt::MatchFlag>::IsEnum);
QVERIFY(!QTestTypeInfo<Qt::MatchFlags>::IsEnum);
}
static int indicator = 0;
// this is a silly C function
extern "C" {
void someCFunc(void *) { indicator = 42; }
}
// this is the catch-template that will be called if the C function doesn't exist
template <typename T>
void someCFunc(T *) { indicator = 10; }
void tst_Compiler::overrideCFunction()
{
someCFunc((void*)0);
QCOMPARE(indicator, 42);
}
#ifndef DONT_TEST_STL_SORTING
void tst_Compiler::stdSortQList()
{
QList<int> list;
list << 4 << 2;
std::sort(list.begin(), list.end());
QCOMPARE(list.value(0), 2);
QCOMPARE(list.value(1), 4);
QList<QString> slist;
slist << "b" << "a";
std::sort(slist.begin(), slist.end());
QCOMPARE(slist.value(0), QString("a"));
QCOMPARE(slist.value(1), QString("b"));
}
void tst_Compiler::stdSortQVector()
{
QVector<int> vector;
vector << 4 << 2;
std::sort(vector.begin(), vector.end());
QCOMPARE(vector.value(0), 2);
QCOMPARE(vector.value(1), 4);
QVector<QString> strvec;
strvec << "b" << "a";
std::sort(strvec.begin(), strvec.end());
QCOMPARE(strvec.value(0), QString("a"));
QCOMPARE(strvec.value(1), QString("b"));
}
#else
void tst_Compiler::stdSortQList()
{ QSKIP("Compiler's STL broken"); }
void tst_Compiler::stdSortQVector()
{ QSKIP("Compiler's STL broken"); }
#endif
// the C func will set it to 1, the template to 2
static int whatWasCalled = 0;
void callOrderFunc(void *)
{
whatWasCalled = 1;
}
template <typename T>
void callOrderFunc(T *)
{
whatWasCalled = 2;
}
template <typename T>
void callOrderNoCFunc(T *)
{
whatWasCalled = 3;
}
/*
This test will check what will get precendence - the C function
or the template.
It also makes sure this template "override" will compile on all systems
and not result in ambiguities.
*/
void tst_Compiler::templateCallOrder()
{
QCOMPARE(whatWasCalled, 0);
// call it with a void *
void *f = 0;
callOrderFunc(f);
QCOMPARE(whatWasCalled, 1);
whatWasCalled = 0;
char *c = 0;
/* call it with a char * - AMBIGOUS, fails on several compilers
callOrderFunc(c);
QCOMPARE(whatWasCalled, 1);
whatWasCalled = 0;
*/
// now try the case when there is no C function
callOrderNoCFunc(f);
QCOMPARE(whatWasCalled, 3);
whatWasCalled = 0;
callOrderNoCFunc(c);
QCOMPARE(whatWasCalled, 3);
whatWasCalled = 0;
}
// test to see if removing =0 from a pure virtual function is BC
void tst_Compiler::virtualFunctionNoLongerPureVirtual()
{
#ifdef BASECLASS_NOT_ABSTRACT
// has a single virtual function, not pure virtual, can call it
BaseClass baseClass;
QTest::ignoreMessage(QtDebugMsg, "BaseClass::wasAPureVirtualFunction()");
baseClass.wasAPureVirtualFunction();
#endif
// DerivedClass inherits from BaseClass, and function is declared
// pure virtual, make sure we can still call it
DerivedClass derivedClass;
QTest::ignoreMessage(QtDebugMsg, "DerivedClass::wasAPureVirtualFunction()");
derivedClass.wasAPureVirtualFunction();
}
template<typename T> const char *resolveCharSignedness();
template<>
const char *resolveCharSignedness<char>()
{
return "char";
}
template<>
const char *resolveCharSignedness<unsigned char>()
{
return "unsigned char";
}
template<>
const char *resolveCharSignedness<signed char>()
{
return "signed char";
}
void tst_Compiler::charSignedness() const
{
QCOMPARE("char", resolveCharSignedness<char>());
QCOMPARE("unsigned char", resolveCharSignedness<unsigned char>());
QCOMPARE("signed char", resolveCharSignedness<signed char>());
}
class PrivateStaticTemplateMember
{
public:
long regularMember()
{
return helper<long, int>(3);
}
private:
template<typename A, typename B>
static A helper(const B b)
{
return A(b);
}
};
void tst_Compiler::privateStaticTemplateMember() const
{
PrivateStaticTemplateMember v;
QCOMPARE(long(3), v.regularMember());
}
#if !defined(Q_CC_MIPS)
// make sure we can use a static initializer with a union and then use
// the second member of the union
static const union { unsigned char c[8]; double d; } qt_be_inf_bytes = { { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 } };
static const union { unsigned char c[8]; double d; } qt_le_inf_bytes = { { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f } };
static inline double qt_inf()
{
return (QSysInfo::ByteOrder == QSysInfo::BigEndian
? qt_be_inf_bytes.d
: qt_le_inf_bytes.d);
}
#else
static const unsigned char qt_be_inf_bytes[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 };
static const unsigned char qt_le_inf_bytes[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f };
static inline double qt_inf()
{
const uchar *bytes;
bytes = (QSysInfo::ByteOrder == QSysInfo::BigEndian
? qt_be_inf_bytes
: qt_le_inf_bytes);
union { uchar c[8]; double d; } returnValue;
memcpy(returnValue.c, bytes, sizeof(returnValue.c));
return returnValue.d;
}
#endif
void tst_Compiler::staticConstUnionWithInitializerList() const
{
double d = qt_inf();
QVERIFY(qIsInf(d));
}
#ifndef Q_NO_TEMPLATE_FRIENDS
template <typename T> class TemplateFriends
{
T value;
public:
TemplateFriends(T value) : value(value) {}
template <typename X> void copy(TemplateFriends<X> other)
{ value = other.value; }
template <typename X> friend class TemplateFriends;
};
void tst_Compiler::templateFriends()
{
TemplateFriends<int> ti(42);
TemplateFriends<long> tl(0);
tl.copy(ti);
}
#else
void tst_Compiler::templateFriends()
{
QSKIP("Compiler does not support template friends");
}
#endif
void tst_Compiler::cxx11_alignas()
{
#ifndef Q_COMPILER_ALIGNAS
QSKIP("Compiler does not support C++11 feature");
#else
struct S {
alignas(double) char c;
};
QCOMPARE(Q_ALIGNOF(S), Q_ALIGNOF(double));
#endif
}
void tst_Compiler::cxx11_alignof()
{
#ifndef Q_COMPILER_ALIGNOF
QSKIP("Compiler does not support C++11 feature");
#else
size_t alignchar = alignof(char);
size_t aligndouble = alignof(double);
QVERIFY(alignchar >= 1);
QVERIFY(alignchar <= aligndouble);
#endif
}
void tst_Compiler::cxx11_alignas_alignof()
{
#if !defined(Q_COMPILER_ALIGNAS) && !defined(Q_COMPILER_ALIGNOF)
QSKIP("Compiler does not support C++11 feature");
#else
alignas(alignof(double)) char c;
Q_UNUSED(c);
#endif
}
void tst_Compiler::cxx11_atomics()
{
#ifndef Q_COMPILER_ATOMICS
QSKIP("Compiler does not support C++11 feature");
#else
std::atomic<int> i;
i.store(42, std::memory_order_seq_cst);
QCOMPARE(i.load(std::memory_order_acquire), 42);
std::atomic<short> s;
s.store(42);
QCOMPARE(s.load(), short(42));
std::atomic_flag flag;
flag.clear();
QVERIFY(!flag.test_and_set());
QVERIFY(flag.test_and_set());
#endif
}
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wignored-attributes")
QT_WARNING_DISABLE_CLANG("-Wunused-local-typedefs")
QT_WARNING_DISABLE_GCC("-Wattributes")
QT_WARNING_DISABLE_GCC("-Wunused-local-typedefs")
#ifndef __has_cpp_attribute
# define __has_cpp_attribute(x) 0
#endif
#ifdef Q_COMPILER_ATTRIBUTES
[[noreturn]] void attribute_f1();
void attribute_f2 [[noreturn]] ();
# if (defined(__cpp_namespace_attributes) && __cpp_namespace_attributes >= 201411) && __has_cpp_attribute(deprecated)
namespace [[deprecated]] NS { }
# endif
#endif
void tst_Compiler::cxx11_attributes()
{
#ifndef Q_COMPILER_ATTRIBUTES
QSKIP("Compiler does not support C++11 feature");
#else
// Attributes in function parameters and using clauses cause MSVC 2015 to crash
// https://connect.microsoft.com/VisualStudio/feedback/details/2011594
# if (!defined(Q_CC_MSVC) || _MSC_FULL_VER >= 190023811) && !defined(Q_CC_INTEL)
void f([[ ]] int);
[[ ]] using namespace QtPrivate;
[[ ]] try {
} catch ([[]] int) {
}
# endif
struct [[ ]] A { };
struct B : A {
[[ ]] int m_i : 32;
[[noreturn]] void f() const { ::exit(0); }
# ifdef Q_COMPILER_DEFAULT_DELETE_MEMBERS
[[ ]] ~B() = default;
[[ ]] B(const B &) = delete;
# endif
};
# if __has_cpp_attribute(deprecated)
struct [[deprecated]] C { };
# endif
enum [[ ]] E { };
[[ ]] void [[ ]] * [[ ]] * [[ ]] ptr = 0;
int B::* [[ ]] pmm = 0;
# if __has_cpp_attribute(deprecated)
enum [[deprecated]] E2 {
# if defined(__cpp_enumerator_attributes) && __cpp_enumerator_attributes >= 201411
value [[deprecated]] = 0
# endif
};
# endif
# ifdef Q_COMPILER_LAMBDA
[]()[[ ]] {}();
# endif
# ifdef Q_COMPILER_TEMPLATE_ALIAS
using B2 [[ ]] = B;
# endif
[[ ]] goto end;
# ifdef Q_CC_GNU
// Attributes in gnu:: namespace
[[gnu::unused]] end:
;
[[gnu::unused]] struct D {} d;
struct D e [[gnu::used, gnu::unused]];
[[gnu::aligned(8)]] int i [[ ]];
int array[][[]] = { 1 };
# else
// Non GNU, so use an empty attribute
[[ ]] end:
;
[[ ]] struct D {} d;
struct D e [[ ]];
[[ ]] int i [[ ]];
int array[][[]] = { 1 };
# endif
int & [[ ]] lref = i;
int && [[ ]] rref = 1;
[[ ]] (void)1;
[[ ]] for (i = 0; i < 2; ++i)
;
Q_UNUSED(ptr);
Q_UNUSED(pmm);
Q_UNUSED(d);
Q_UNUSED(e);
Q_UNUSED(i);
Q_UNUSED(array);
Q_UNUSED(lref);
Q_UNUSED(rref);
#endif
}
QT_WARNING_POP
#ifdef Q_COMPILER_AUTO_FUNCTION
auto autoFunction() -> unsigned
{
return 1;
}
#endif
void tst_Compiler::cxx11_auto_function()
{
#ifndef Q_COMPILER_AUTO_FUNCTION
QSKIP("Compiler does not support C++11 feature");
#else
QCOMPARE(autoFunction(), 1u);
#endif
}
void tst_Compiler::cxx11_auto_type()
{
#ifndef Q_COMPILER_AUTO_TYPE
QSKIP("Compiler does not support C++11 feature");
#else
auto i = 1;
auto x = QRandomGenerator::global()->generate();
auto l = 1L;
auto s = QStringLiteral("Hello World");
QCOMPARE(i, 1);
Q_UNUSED(x);
QCOMPARE(l, 1L);
QCOMPARE(s.toLower(), QString("hello world"));
#endif
}
void tst_Compiler::cxx11_class_enum()
{
#ifndef Q_COMPILER_CLASS_ENUM
QSKIP("Compiler does not support C++11 feature");
#else
enum class X { EnumValue };
X x = X::EnumValue;
QCOMPARE(x, X::EnumValue);
enum class Y : short { Val = 2 };
enum Z : long { ValLong = LONG_MAX };
#endif
}
#ifdef Q_COMPILER_CONSTEXPR
constexpr int constexprValue()
{
return 42;
}
#endif
void tst_Compiler::cxx11_constexpr()
{
#ifndef Q_COMPILER_CONSTEXPR
QSKIP("Compiler does not support C++11 feature");
#else
static constexpr QBasicAtomicInt atomic = Q_BASIC_ATOMIC_INITIALIZER(1);
static constexpr int i = constexprValue();
QCOMPARE(i, constexprValue());
QCOMPARE(atomic.loadRelaxed(), 1);
#endif
}
void tst_Compiler::cxx11_decltype()
{
#ifndef Q_COMPILER_DECLTYPE
QSKIP("Compiler does not support C++11 feature");
#else
decltype(QRandomGenerator::global()->generate()) i = 0;
QCOMPARE(i, 0U);
#endif
}
void tst_Compiler::cxx11_default_members()
{
#ifndef Q_COMPILER_DEFAULT_MEMBERS
QSKIP("Compiler does not support C++11 feature");
#else
class DefaultMembers
{
protected:
DefaultMembers() = default;
public:
DefaultMembers(int) {}
};
class DefaultMembersChild: public DefaultMembers
{
DefaultMembersChild(const DefaultMembersChild &) : DefaultMembers() {}
public:
DefaultMembersChild():DefaultMembers() {};
DefaultMembersChild(DefaultMembersChild &&) = default;
};
DefaultMembersChild dm;
DefaultMembersChild dm2 = std::move(dm);
Q_UNUSED(dm2);
#endif
}
void tst_Compiler::cxx11_delete_members()
{
#ifndef Q_COMPILER_DELETE_MEMBERS
QSKIP("Compiler does not support C++11 feature");
#else
class DeleteMembers
{
protected:
DeleteMembers() = delete;
public:
DeleteMembers(const DeleteMembers &) = delete;
~DeleteMembers() = delete;
};
#endif
}
void tst_Compiler::cxx11_delegating_constructors()
{
#ifndef Q_COMPILER_DELEGATING_CONSTRUCTORS
QSKIP("Compiler does not support C++11 feature");
#else
struct DC {
DC(int i) : i(i) {}
DC() : DC(0) {}
int i;
};
DC dc;
QCOMPARE(dc.i, 0);
#endif
}
void tst_Compiler::cxx11_explicit_conversions()
{
#ifndef Q_COMPILER_EXPLICIT_CONVERSIONS
QSKIP("Compiler does not support C++11 feature");
#else
struct EC {
explicit operator int() const { return 0; }
operator long long() const { return 1; }
};
EC ec;
int i(ec);
QCOMPARE(i, 0);
int i2 = ec;
QCOMPARE(i2, 1);
#endif
}
void tst_Compiler::cxx11_explicit_overrides()
{
#ifndef Q_COMPILER_EXPLICIT_OVERRIDES
QSKIP("Compiler does not support C++11 feature");
#else
struct Base {
virtual ~Base() {}
virtual void f() {}
};
struct Derived final : public Base {
virtual void f() final override {}
};
#endif
}
#ifdef Q_COMPILER_EXTERN_TEMPLATES
template <typename T> T externTemplate() { return T(0); }
extern template int externTemplate<int>();
#endif
void tst_Compiler::cxx11_extern_templates()
{
#ifndef Q_COMPILER_EXTERN_TEMPLATES
QSKIP("Compiler does not support C++11 feature");
#else
QCOMPARE(externTemplate<int>(), 42);
#endif
}
void tst_Compiler::cxx11_inheriting_constructors()
{
#ifndef Q_COMPILER_INHERITING_CONSTRUCTORS
QSKIP("Compiler does not support C++11 feature");
#else
struct Base {
int i;
Base() : i(0) {}
Base(int i) : i(i) {}
};
struct Derived : public Base {
using Base::Base;
};
Derived d(1);
QCOMPARE(d.i, 1);
#endif
}
void tst_Compiler::cxx11_initializer_lists()
{
#ifndef Q_COMPILER_INITIALIZER_LISTS
QSKIP("Compiler does not support C++11 feature");
#else
QList<int> l = { 1, 2, 3, 4, 5 };
QCOMPARE(l.length(), 5);
QCOMPARE(l.at(0), 1);
QCOMPARE(l.at(4), 5);
#endif
}
struct CallFunctor
{
template <typename Functor> static int f(Functor f)
{ return f();}
};
void tst_Compiler::cxx11_lambda()
{
#ifndef Q_COMPILER_LAMBDA
QSKIP("Compiler does not support C++11 feature");
#else
QCOMPARE(CallFunctor::f([]() { return 42; }), 42);
#endif
}
void tst_Compiler::cxx11_nonstatic_member_init()
{
#ifndef Q_COMPILER_NONSTATIC_MEMBER_INIT
QSKIP("Compiler does not support C++11 feature");
#else
struct S {
int i = 42;
long l = 47;
char c;
S() : l(-47), c(0) {}
};
S s;
QCOMPARE(s.i, 42);
QCOMPARE(s.l, -47L);
QCOMPARE(s.c, '\0');
#endif
}
void tst_Compiler::cxx11_noexcept()
{
#ifndef Q_COMPILER_NOEXCEPT
QSKIP("Compiler does not support C++11 feature");
#else
extern void noexcept_f() noexcept;
extern void g() noexcept(noexcept(noexcept_f()));
QCOMPARE(noexcept(cxx11_noexcept()), false);
QCOMPARE(noexcept(noexcept_f), true);
QCOMPARE(noexcept(g), true);
#endif
}
void tst_Compiler::cxx11_nullptr()
{
#ifndef Q_COMPILER_NULLPTR
QSKIP("Compiler does not support C++11 feature");
#else
void *v = nullptr;
char *c = nullptr;
const char *cc = nullptr;
volatile char *vc = nullptr;
std::nullptr_t np = nullptr;
v = np;
Q_UNUSED(v);
Q_UNUSED(c);
Q_UNUSED(cc);
Q_UNUSED(vc);
#endif
}
namespace SomeNamespace {
class AdlOnly {
QVector<int> v;
public:
AdlOnly() : v(5) { std::fill_n(v.begin(), v.size(), 42); }
private:
friend QVector<int>::const_iterator begin(const AdlOnly &x) { return x.v.begin(); }
friend QVector<int>::const_iterator end(const AdlOnly &x) { return x.v.end(); }
friend QVector<int>::iterator begin(AdlOnly &x) { return x.v.begin(); }
friend QVector<int>::iterator end(AdlOnly &x) { return x.v.end(); }
};
}
void tst_Compiler::cxx11_range_for()
{
#ifndef Q_COMPILER_RANGE_FOR
QSKIP("Compiler does not support C++11 feature");
#else
QList<int> l;
l << 1 << 2 << 3;
for (int i : l)
Q_UNUSED(i);
l.clear();
l << 1;
for (int i : l)
QCOMPARE(i, 1);
QList<long> ll;
l << 2;
for (int i : ll)
QCOMPARE(i, 2);
{
const int array[] = { 0, 1, 2, 3, 4 };
int i = 0;
for (const int &e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (int e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (const int e : array)
QCOMPARE(e, array[i++]);
#ifdef Q_COMPILER_AUTO_TYPE
i = 0;
for (const auto &e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (auto &e : array) // auto deducing const
QCOMPARE(e, array[i++]);
i = 0;
for (auto e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (const auto e : array)
QCOMPARE(e, array[i++]);
#endif
}
{
int array[] = { 0, 1, 2, 3, 4 };
const int array2[] = { 10, 11, 12, 13, 14 };
int i = 0;
for (const int &e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (int &e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (int e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (const int e : array)
QCOMPARE(e, array[i++]);
#ifdef Q_COMPILER_AUTO_TYPE
i = 0;
for (const auto &e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (auto &e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (auto e : array)
QCOMPARE(e, array[i++]);
i = 0;
for (const auto e : array)
QCOMPARE(e, array[i++]);
#endif
for (int &e : array)
e += 10;
i = 0;
for (const int &e : array)
QCOMPARE(e, array2[i++]);
}
{
const SomeNamespace::AdlOnly x;
for (const int &e : x)
QCOMPARE(e, 42);
}
{
SomeNamespace::AdlOnly x;
for (const int &e : x)
QCOMPARE(e, 42);
for (int &e : x)
e += 10;
for (const int &e : x)
QCOMPARE(e, 52);
}
#endif
}
void tst_Compiler::cxx11_raw_strings()
{
#ifndef Q_COMPILER_RAW_STRINGS
QSKIP("Compiler does not support C++11 feature");
#else
static const char xml[] = R"(<?xml version="1.0" encoding="UTF-8" ?>)";
static const char raw[] = R"***(*"*)***";
QCOMPARE(strlen(raw), size_t(3));
QCOMPARE(raw[1], '"');
Q_UNUSED(xml);
#endif
}
void tst_Compiler::cxx11_ref_qualifiers()
{
#ifndef Q_COMPILER_REF_QUALIFIERS
QSKIP("Compiler does not support C++11 feature");
#else
# ifndef Q_COMPILER_RVALUE_REFS
# error "Impossible condition: ref qualifiers are supported but not rvalue refs"
# endif
// also applies to function pointers
QByteArray (QString:: *lvalueref)() const & = &QString::toLatin1;
QByteArray (QString:: *rvalueref)() && = &QString::toLatin1;
QString s("Hello");
QCOMPARE((s.*lvalueref)(), QByteArray("Hello"));
QCOMPARE((std::move(s).*rvalueref)(), QByteArray("Hello"));
// tests internal behavior:
QVERIFY(s.isEmpty());
#endif
}
class MoveDefinedQString {
QString s;
public:
MoveDefinedQString() : s() {}
explicit MoveDefinedQString(const QString &s) : s(s) {}
MoveDefinedQString(const MoveDefinedQString &other) : s(other.s) {}
#ifdef Q_COMPILER_RVALUE_REFS
MoveDefinedQString(MoveDefinedQString &&other) : s(std::move(other.s)) { other.s.clear(); }
MoveDefinedQString &operator=(MoveDefinedQString &&other)
{ s = std::move(other.s); other.s.clear(); return *this; }
#endif
MoveDefinedQString &operator=(const MoveDefinedQString &other) { s = other.s; return *this; }
private:
friend bool operator==(const MoveDefinedQString &lhs, const MoveDefinedQString &rhs)
{ return lhs.s == rhs.s; }
friend bool operator!=(const MoveDefinedQString &lhs, const MoveDefinedQString &rhs)
{ return !operator==(lhs, rhs); }
friend char* toString(const MoveDefinedQString &mds)
{ using namespace QTest; return toString(mds.s); }
};
void tst_Compiler::cxx11_rvalue_refs()
{
#ifndef Q_COMPILER_RVALUE_REFS
QSKIP("Compiler does not support C++11 feature");
#else
// we require std::move:
{
int i = 1;
i = std::move(i);
MoveDefinedQString s("Hello");
MoveDefinedQString t = std::move(s);
QCOMPARE(t, MoveDefinedQString("Hello"));
QCOMPARE(s, MoveDefinedQString());
s = t;
t = std::move(s);
QCOMPARE(t, MoveDefinedQString("Hello"));
QCOMPARE(s, MoveDefinedQString());
MoveDefinedQString &&r = std::move(t); // no actual move!
QCOMPARE(r, MoveDefinedQString("Hello"));
QCOMPARE(t, MoveDefinedQString("Hello")); // so 't' is unchanged
}
// we require std::forward:
{
MoveDefinedQString s("Hello");
MoveDefinedQString s2 = std::forward<MoveDefinedQString>(s); // forward as rvalue
QCOMPARE(s2, MoveDefinedQString("Hello"));
QCOMPARE(s, MoveDefinedQString());
MoveDefinedQString s3 = std::forward<MoveDefinedQString&>(s2); // forward as lvalue
QCOMPARE(s2, MoveDefinedQString("Hello"));
QCOMPARE(s3, MoveDefinedQString("Hello"));
}
// we require automatic generation of move special member functions:
{
struct M { MoveDefinedQString s1, s2; };
M m1 = { MoveDefinedQString("Hello"), MoveDefinedQString("World") };
QCOMPARE(m1.s1, MoveDefinedQString("Hello"));
QCOMPARE(m1.s2, MoveDefinedQString("World"));
M m2 = std::move(m1);
QCOMPARE(m1.s1, MoveDefinedQString());
QCOMPARE(m1.s2, MoveDefinedQString());
QCOMPARE(m2.s1, MoveDefinedQString("Hello"));
QCOMPARE(m2.s2, MoveDefinedQString("World"));
M m3;
QCOMPARE(m3.s1, MoveDefinedQString());
QCOMPARE(m3.s2, MoveDefinedQString());
m3 = std::move(m2);
QCOMPARE(m2.s1, MoveDefinedQString());
QCOMPARE(m2.s2, MoveDefinedQString());
QCOMPARE(m3.s1, MoveDefinedQString("Hello"));
QCOMPARE(m3.s2, MoveDefinedQString("World"));
}
#endif
}
void tst_Compiler::cxx11_static_assert()
{
#ifndef Q_COMPILER_STATIC_ASSERT
QSKIP("Compiler does not support C++11 feature");
#else
static_assert(true, "Message");
#endif
}
#ifdef Q_COMPILER_TEMPLATE_ALIAS
template <typename T> using Map = QMap<QString, T>;
#endif
void tst_Compiler::cxx11_template_alias()
{
#ifndef Q_COMPILER_TEMPLATE_ALIAS
QSKIP("Compiler does not support C++11 feature");
#else
Map<QVariant> m;
m.insert("Hello", "World");
QCOMPARE(m.value("Hello").toString(), QString("World"));
using X = int;
X i = 0;
Q_UNUSED(i);
#endif
}
#ifdef Q_COMPILER_THREAD_LOCAL
static thread_local int stl = 42;
thread_local int gtl = 42;
#endif
void tst_Compiler::cxx11_thread_local()
{
#ifndef Q_COMPILER_THREAD_LOCAL
QSKIP("Compiler does not support C++11 feature");
#else
thread_local int v = 1;
QVERIFY(v);
QVERIFY(stl);
QVERIFY(gtl);
thread_local QString s = "Hello";
QVERIFY(!s.isEmpty());
#endif
}
#ifdef Q_COMPILER_UDL
QString operator"" _tstqstring(const char *str, size_t len)
{
return QString::fromUtf8(str, len) + " UDL";
}
#endif
void tst_Compiler::cxx11_udl()
{
#ifndef Q_COMPILER_UDL
QSKIP("Compiler does not support C++11 feature");
#else
QString s = "Hello World"_tstqstring;
QCOMPARE(s, QString("Hello World UDL"));
#endif
}
void tst_Compiler::cxx11_unicode_strings()
{
#ifndef Q_COMPILER_UNICODE_STRINGS
QSKIP("Compiler does not support C++11 feature");
#else
static const char16_t u[] = u"\u200BHello\u00A0World";
QCOMPARE(u[0], char16_t(0x200B));
static const char32_t U[] = U"\ufffe";
QCOMPARE(U[0], char32_t(0xfffe));
QCOMPARE(u"\U00010000"[0], char16_t(0xD800));
QCOMPARE(u"\U00010000"[1], char16_t(0xDC00));
#endif
}
static void noop(QPair<int, int>) {}
void tst_Compiler::cxx11_uniform_init()
{
#ifndef Q_COMPILER_UNIFORM_INIT
QSKIP("Compiler does not support C++11 feature");
noop(QPair<int,int>());
#else
QString s{"Hello"};
int i{};
noop(QPair<int,int>{1,1});
noop({i,1});
#endif
}
void tst_Compiler::cxx11_unrestricted_unions()
{
#ifndef Q_COMPILER_UNRESTRICTED_UNIONS
QSKIP("Compiler does not support C++11 feature");
#else
union U {
QString s;
U() {}
U(const QString &s) : s(s) {}
~U() {}
};
U u;
std::aligned_storage<sizeof(QString), Q_ALIGNOF(QString)> as;
Q_UNUSED(u);
Q_UNUSED(as);
U u2("hello");
u2.s.~QString();
#endif
}
void tst_Compiler::cxx11_variadic_macros()
{
#ifndef Q_COMPILER_VARIADIC_MACROS
QSKIP("Compiler does not support C++11 feature");
#else
# define TEST_VARARG(x, ...) __VA_ARGS__
QCOMPARE(TEST_VARARG(0, 1), 1);
#endif
}
#ifdef Q_COMPILER_VARIADIC_TEMPLATES
template <typename... Args> struct VariadicTemplate {};
#endif
void tst_Compiler::cxx11_variadic_templates()
{
#ifndef Q_COMPILER_VARIADIC_TEMPLATES
QSKIP("Compiler does not support C++11 feature");
#else
VariadicTemplate<> v0;
VariadicTemplate<int> v1;
VariadicTemplate<int, int, int, int,
int, int, int, int> v8;
Q_UNUSED(v0);
Q_UNUSED(v1);
Q_UNUSED(v8);
#endif
}
void tst_Compiler::cxx14_binary_literals()
{
#if __cpp_binary_literals-0 < 201304
QSKIP("Compiler does not support this C++14 feature");
#else
int i = 0b11001001;
QCOMPARE(i, 0xC9);
#endif
}
void tst_Compiler::cxx14_init_captures()
{
#if __cpp_init_captures-0 < 201304
QSKIP("Compiler does not support this C++14 feature");
#else
QCOMPARE([x = 42]() { return x; }(), 42);
#endif
}
void tst_Compiler::cxx14_generic_lambdas()
{
#if __cpp_generic_lambdas-0 < 201304
QSKIP("Compiler does not support this C++14 feature");
#else
auto identity = [](auto x) { return x; };
QCOMPARE(identity(42), 42);
QCOMPARE(identity(42U), 42U);
QCOMPARE(identity(42L), 42L);
#endif
}
#if __cpp_constexpr-0 >= 201304
constexpr int relaxedConstexpr(int i)
{
i *= 2;
i += 2;
return i;
}
#endif
void tst_Compiler::cxx14_constexpr()
{
#if __cpp_constexpr-0 < 201304
QSKIP("Compiler does not support this C++14 feature");
#else
QCOMPARE(relaxedConstexpr(0), 2);
QCOMPARE(relaxedConstexpr(2), 6);
#endif
}
void tst_Compiler::cxx14_decltype_auto()
{
#if __cpp_decltype_auto-0 < 201304
QSKIP("Compiler does not support this C++14 feature");
#else
QList<int> l;
l << 1;
decltype(auto) value = l[0];
value = 2;
QCOMPARE(l.at(0), 2);
#endif
}
#if __cpp_return_type_deduction >= 201304
auto returnTypeDeduction(bool choice)
{
if (choice)
return 1U;
return returnTypeDeduction(!choice);
}
#endif
void tst_Compiler::cxx14_return_type_deduction()
{
#if __cpp_return_type_deduction-0 < 201304
QSKIP("Compiler does not support this C++14 feature");
#else
QCOMPARE(returnTypeDeduction(false), 1U);
#endif
}
void tst_Compiler::cxx14_aggregate_nsdmi()
{
#if __cpp_aggregate_nsdmi-0 < 201304
QSKIP("Compiler does not support this C++14 feature");
#else
struct S { int i, j = i; };
S s = { 1 };
QCOMPARE(s.j, 1);
#endif
}
#if __cpp_variable_templates >= 201304
template <typename T> constexpr T variableTemplate = T(42);
#endif
void tst_Compiler::cxx14_variable_templates()
{
#if __cpp_variable_templates-0 < 201304
QSKIP("Compiler does not support this C++14 feature");
#else
QCOMPARE(variableTemplate<int>, 42);
QCOMPARE(variableTemplate<long>, 42L);
QCOMPARE(variableTemplate<unsigned>, 42U);
QCOMPARE(variableTemplate<unsigned long long>, 42ULL);
#endif
}
void tst_Compiler::runtimeArrays()
{
#if __cpp_runtime_arrays-0 < 201304
QSKIP("Compiler does not support this C++14 feature");
#else
int i[QRandomGenerator::global()->generate() & 0x1f];
Q_UNUSED(i);
#endif
}
QTEST_MAIN(tst_Compiler)
#include "tst_compiler.moc"
|