Knowledge

decltype

Source 📝

190:-ness of the expression was deemed too confusing. The initial proposal to the C++ standards committee outlined a combination of the two variants; the operator would return a reference type only if the declared type of the expression included a reference. To emphasize that the deduced type would reflect the "declared type" of the expression, the operator was proposed to be named 462:
s and class member access expressions. Example: Note for added lines for bar(). Below the type deduced for "bar()" is plain int, not const int, because prvalues of non-class types always have cv-unqualified types, despite the statically declared different type.
162:, as a more powerful and general technique. However, Steve Dewhurst argued that such conventions are "costly to design and promulgate", and that it would be "much easier to ... simply extract the type of the expression." In a 2011 article on 173:
suggested extending the C++ language with mechanisms for querying the type of an expression, and initializing objects without specifying the type. Stroustrup observed that the reference-dropping semantics offered by the
80:'s semantics were designed to cater to both generic library writers and novice programmers. In general, the deduced type matches the type of the object or function exactly as declared in the source code. Like the 145:
remarked that "having a typeof would make much template code easier to write and understand." He also noted that "typeof and sizeof share the same backend, because sizeof has to compute the type anyway."
63:
techniques became increasingly popular throughout the 1990s, the need for a type-deduction mechanism was recognized. Many compiler vendors implemented their own versions of the operator, typically called
1411: 118:, was recognized. In generic programming, it is often difficult or impossible to express types that depend on template parameters, in particular the return type of function template instantiations. 74:
proposed that a standardized version of the operator be added to the C++ language, and suggested the name "decltype", to reflect that the operator would yield the "declared type" of an expression.
154:
facility, with the caveat that "using it often invites subtle programming errors, and there are some problems that it cannot solve." They characterized the use of type conventions, like the
715:
member body noted that "a scope operator(::) cannot be applied to decltype, but it should be. It would be useful in the case to obtain member type(nested-type) from an instance as follows:
205:
templates. It is sometimes desirable to write a generic forwarding function that returns the same type as the wrapped function, regardless of the type it is instantiated with. Without
680:
nor a member access expression, and therefore does not denote a named object. Because the expression is an lvalue, its deduced type is "reference to the type of the expression", or
454:
These semantics were designed to fulfill the needs of generic library writers, while at the same time being intuitive for novice programmers, because the return type of
1436: 1415: 131:
operator. His work was expanded on by Bill Gibbons, who concluded that the technique had several limitations and was generally less powerful than an actual
385:
refers to a variable in local or namespace scope, a static member variable or a function parameter, then the result is that variable's or parameter's
684:. The fact that extra parentheses introduce a reference qualifier to the type can be a source of errors for programmers who do not fully understand 1461: 712: 125:
operator as a compiler extension. As early as 1997, before C++ was fully standardized, Brian Parker proposed a portable solution based on the
458:
always matches the type of the object or function exactly as declared in the source code. More formally, Rule 1 applies to unparenthesized
70:, and some portable implementations with limited functionality, based on existing language features were developed. In 2002, 812:
type specifier that closely mimics the semantics as described in the standards committee proposal. It can be used with both
816:
and native code. The documentation states that it is "useful primarily to developers who write template libraries."
351:
is essential here because it preserves the information about whether the wrapped function returns a reference type.
782: 166:, Koenig and Moo predicted that "decltype will be widely used to make everyday programs easier to write." 778: 1120: 147: 1465: 1315: 1293: 1225: 1203: 159: 105: 1337: 1268: 1178: 1148: 1091: 899: 1390: 1365: 420: 412: 396: 187: 109: 41: 691:
In December 2008, a concern was raised to the committee by Jaakko Järvi over the inability to use
821: 179: 186:
compilers could be problematic. Conversely, an operator returning a reference type based on the
442:
but it preserves the value category of the initializer. More specifically, it is equivalent to
97: 864: 1065: 1040: 1015: 987: 959: 137: 837: 785:
call, were addressed by David Vandevoorde, and voted into the working paper in March 2010.
211: 23: 8: 183: 142: 101: 60: 49: 924: 209:, it is not generally possible to accomplish this. An example, which also utilizes the 1247: 1440: 170: 71: 373:
will not result in an increment of the variable i. Informally, the type returned by
1481: 202: 53: 52:, where it is often difficult, or even impossible, to express types that depend on 897: 438: 1498: 898:
Gregor, Douglas; Järvi, Jaakko; Siek, Jeremy; Stroustrup, Bjarne (2003-04-28).
432: 1492: 833: 33: 805: 1361: 1061: 773:
This, and similar issues pertaining to the wording inhibiting the use of
1266: 1146: 1089: 1124: 813: 801: 37: 668:
The reason for the difference between the latter two invocations of
1267:
Dos Reis, Gabriel; Järvi, Jaakko; Stroustrup, Bjarne (2006-11-05).
1147:
Dos Reis, Gabriel; Järvi, Jaakko; Stroustrup, Bjarne (2007-07-18).
1090:
Dos Reis, Gabriel; Järvi, Jaakko; Stroustrup, Bjarne (2004-10-12).
1036: 1011: 983: 955: 829: 797: 708: 163: 155: 45: 150:
and Barbara E. Moo also recognized the usefulness of a built-in
1386: 984:"Generic<Programming>: Mappings between Types and Values" 361: 127: 114: 66: 841: 20: 800:. It is provided by a number of compilers as an extension. 1338:"Core issues 743 and 950: Additional decltype(...) uses" 1316:"C++ Standard Core Language Defect Reports, Revision 69" 1204:"C++ Standard Core Language Defect Reports, Revision 65" 824:
C++ compiler in version 4.3, released on March 5, 2008.
108:, the need for a mechanism for obtaining the type of an 1226:"C++ Standard Core Language Closed Issues, Revision 65" 865:"C++ Standard Core Language Active Issues, Revision 66" 1343:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 1318:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 1296:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 1274:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 1228:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 1206:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 1184:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 1179:"Working Draft, Standard for Programming Language C++" 1154:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 1097:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 905:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 867:. ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee 707:". While commenting on the formal Committee Draft for 1037:"Common Knowledge: A Bitwise typeof Operator, Part 1" 100:
into the C++ programming language, and the advent of
1239: 1059: 1009: 977: 975: 1490: 1482:Bjarne Stroustrup's C++0x FAQ entry for decltype 1115: 1113: 1111: 972: 1248:"C++ value categories and decltype demystified" 858: 856: 796:is included in the C++ Language Standard since 1108: 1060:Koenig, Andrew; Barbara E. Moo (2011-07-19). 1053: 1010:Koenig, Andrew; Barbara E. Moo (2002-02-01). 949: 947: 945: 699:, which is inconsistent with the intent that 1285: 1217: 1005: 1003: 981: 853: 1335: 1414:. Embarcadero Technologies. Archived from 942: 201:proposal was the ability to write perfect 197:One of the cited main motivations for the 1269:"Decltype (revision 6): proposed wording" 1149:"Decltype (revision 7): proposed wording" 1000: 135:mechanism. In an October 2000 article of 1404: 1245: 1172: 1170: 1168: 1142: 1140: 1034: 1028: 1195: 1085: 1083: 1081: 1012:"C++ Made Easier: Naming Unknown Types" 953: 893: 891: 889: 887: 885: 883: 881: 1491: 1459: 1313: 1291: 1223: 1201: 916: 862: 1453: 1429: 1379: 1354: 1329: 1307: 1260: 1165: 1137: 922: 672:is that the parenthesized expression 1462:"New C++0x feature support in Clang" 1078: 925:"Clean Up Function Syntax Mess with 878: 369:is unevaluated, so expressions like 982:Alexandrescu, Andrei (2000-10-01). 703:should be treated "as if it were a 13: 1336:Vandevoorde, Daveed (2010-02-03). 1176: 14: 1510: 1475: 1412:"Type Specifier decltype (C++0x)" 1314:Miller, William M. (2010-03-29). 1292:Miller, William M. (2009-08-03). 1224:Miller, William M. (2009-08-03). 1202:Miller, William M. (2009-08-03). 863:Miller, William M. (2009-09-29). 820:was added to the mainline of the 48:, its primary intended use is in 1092:"Decltype and auto (revision 4)" 1062:"4 Useful New Features in C++0x" 788: 436:allows for type deduction like 1460:Gregor, Douglas (2011-01-26). 1035:Dewhurst, Steve (2000-08-01). 956:"A Portable "typeof" Operator" 808:and later compilers provide a 567:// type is const int&& 1: 1246:Mazières, David (June 2021). 847: 411:is the type of e; if e is an 91: 88:'s operand is not evaluated. 954:Gibbons, Bill (2000-11-01). 663:// type is const double& 354: 104:techniques pioneered by the 7: 923:Kalev, Danny (2008-05-08). 16:Feature introduced in C++11 10: 1515: 112:, commonly referred to as 365:operator, the operand of 178:operator provided by the 160:Standard Template Library 121:Many vendors provide the 106:Standard Template Library 96:With the introduction of 1391:Free Software Foundation 1387:"GCC 4.3 Release Series" 1366:Free Software Foundation 1294:"C++ CD1 Comment Status" 777:in the declaration of a 717: 465: 217: 377:is deduced as follows: 1362:"C++0x Support in GCC" 1125:Microsoft Corporation 283:transparent_forwarder 419:; otherwise, e is a 212:trailing-return-type 24:programming language 1121:"decltype Operator" 900:"Decltype and auto" 828:is also present in 430:As a special case, 203:forwarding function 143:Andrei Alexandrescu 102:generic programming 61:generic programming 50:generic programming 1066:Dr. Dobb's Journal 1041:Dr. Dobb's Journal 1016:Dr. Dobb's Journal 988:Dr. Dobb's Journal 960:Dr. Dobb's Journal 838:Intel C++ Compiler 423:and the result is 381:If the expression 138:Dr. Dobb's Journal 36:used to query the 1441:Intel Corporation 682:const double& 636:// type is double 359:Similarly to the 171:Bjarne Stroustrup 72:Bjarne Stroustrup 1506: 1470: 1469: 1464:. Archived from 1457: 1451: 1450: 1448: 1447: 1433: 1427: 1426: 1424: 1423: 1408: 1402: 1401: 1399: 1398: 1383: 1377: 1376: 1374: 1373: 1358: 1352: 1351: 1349: 1348: 1342: 1333: 1327: 1326: 1324: 1323: 1311: 1305: 1304: 1302: 1301: 1289: 1283: 1282: 1280: 1279: 1273: 1264: 1258: 1257: 1255: 1254: 1243: 1237: 1236: 1234: 1233: 1221: 1215: 1214: 1212: 1211: 1199: 1193: 1192: 1190: 1189: 1183: 1174: 1163: 1162: 1160: 1159: 1153: 1144: 1135: 1134: 1132: 1131: 1117: 1106: 1105: 1103: 1102: 1096: 1087: 1076: 1075: 1073: 1072: 1057: 1051: 1050: 1048: 1047: 1032: 1026: 1025: 1023: 1022: 1007: 998: 997: 995: 994: 979: 970: 969: 967: 966: 951: 940: 939: 937: 936: 920: 914: 913: 911: 910: 904: 895: 876: 875: 873: 872: 860: 834:C++ Builder 2009 827: 819: 811: 795: 776: 769: 766: 763: 760: 757: 754: 751: 748: 745: 742: 739: 736: 733: 730: 727: 724: 721: 702: 694: 687: 683: 675: 671: 664: 661: 658: 655: 652: 649: 646: 643: 640: 637: 634: 631: 628: 625: 622: 619: 616: 613: 610: 607: 604: 601: 598: 595: 592: 589: 586: 583: 580: 577: 574: 571: 568: 565: 562: 559: 556: 553: 550: 547: 544: 541: 538: 535: 532: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 496: 493: 490: 487: 484: 481: 478: 475: 472: 469: 457: 449: 441: 435: 426: 418: 415:, the result is 410: 406: 402: 394: 384: 376: 372: 368: 364: 350: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 308: 305: 302: 299: 296: 293: 290: 287: 284: 281: 278: 275: 272: 269: 266: 263: 260: 257: 254: 251: 248: 245: 242: 239: 236: 233: 230: 227: 224: 221: 208: 200: 193: 177: 158:provided by the 153: 134: 130: 124: 117: 87: 83: 79: 69: 44:. Introduced in 31: 1514: 1513: 1509: 1508: 1507: 1505: 1504: 1503: 1489: 1488: 1478: 1473: 1458: 1454: 1445: 1443: 1435: 1434: 1430: 1421: 1419: 1410: 1409: 1405: 1396: 1394: 1385: 1384: 1380: 1371: 1369: 1360: 1359: 1355: 1346: 1344: 1340: 1334: 1330: 1321: 1319: 1312: 1308: 1299: 1297: 1290: 1286: 1277: 1275: 1271: 1265: 1261: 1252: 1250: 1244: 1240: 1231: 1229: 1222: 1218: 1209: 1207: 1200: 1196: 1187: 1185: 1181: 1175: 1166: 1157: 1155: 1151: 1145: 1138: 1129: 1127: 1119: 1118: 1109: 1100: 1098: 1094: 1088: 1079: 1070: 1068: 1058: 1054: 1045: 1043: 1033: 1029: 1020: 1018: 1008: 1001: 992: 990: 980: 973: 964: 962: 952: 943: 934: 932: 921: 917: 908: 906: 902: 896: 879: 870: 868: 861: 854: 850: 825: 817: 809: 806:Visual C++ 2010 793: 791: 774: 771: 770: 767: 764: 761: 758: 755: 752: 749: 746: 743: 740: 737: 734: 731: 728: 725: 722: 719: 711:, the Japanese 700: 692: 685: 681: 673: 669: 666: 665: 662: 659: 656: 653: 650: 647: 644: 641: 638: 635: 632: 629: 626: 623: 620: 617: 614: 611: 608: 605: 602: 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 467: 455: 443: 437: 431: 424: 416: 408: 404: 400: 392: 382: 374: 370: 366: 360: 357: 348: 346: 345: 342: 339: 336: 333: 330: 327: 324: 321: 318: 315: 312: 309: 306: 303: 300: 297: 294: 291: 288: 285: 282: 279: 276: 273: 270: 267: 264: 261: 258: 255: 252: 249: 246: 243: 240: 237: 234: 231: 228: 225: 222: 219: 206: 198: 191: 175: 151: 132: 126: 122: 113: 94: 85: 81: 77: 65: 27: 17: 12: 11: 5: 1512: 1502: 1501: 1485: 1484: 1477: 1476:External links 1474: 1472: 1471: 1468:on 2011-01-30. 1452: 1428: 1403: 1378: 1353: 1328: 1306: 1284: 1259: 1238: 1216: 1194: 1177:Becker, Pete. 1164: 1136: 1107: 1077: 1052: 1027: 999: 971: 941: 915: 877: 851: 849: 846: 790: 787: 718: 676:is neither an 609:// type is int 588:// type is int 466: 452: 451: 433:decltype(auto) 428: 391:Otherwise, if 389: 356: 353: 218: 93: 90: 15: 9: 6: 4: 3: 2: 1511: 1500: 1497: 1496: 1494: 1487: 1483: 1480: 1479: 1467: 1463: 1456: 1442: 1438: 1432: 1418:on 2011-07-08 1417: 1413: 1407: 1392: 1388: 1382: 1367: 1363: 1357: 1339: 1332: 1317: 1310: 1295: 1288: 1270: 1263: 1249: 1242: 1227: 1220: 1205: 1198: 1180: 1173: 1171: 1169: 1150: 1143: 1141: 1126: 1122: 1116: 1114: 1112: 1093: 1086: 1084: 1082: 1067: 1063: 1056: 1042: 1038: 1031: 1017: 1013: 1006: 1004: 989: 985: 978: 976: 961: 957: 950: 948: 946: 930: 928: 919: 901: 894: 892: 890: 888: 886: 884: 882: 866: 859: 857: 852: 845: 843: 839: 835: 831: 823: 815: 807: 803: 799: 786: 784: 780: 779:derived class 768:// int i = 0; 716: 714: 710: 706: 698: 689: 679: 678:id-expression 464: 461: 460:id-expression 447: 440: 434: 429: 422: 414: 398: 390: 388: 387:declared type 380: 379: 378: 371:decltype(i++) 363: 352: 216: 214: 213: 204: 195: 189: 185: 181: 172: 167: 165: 161: 157: 149: 148:Andrew Koenig 144: 140: 139: 129: 119: 116: 111: 107: 103: 99: 89: 75: 73: 68: 62: 57: 55: 51: 47: 43: 39: 35: 30: 25: 22: 1486: 1466:the original 1455: 1444:. Retrieved 1431: 1420:. Retrieved 1416:the original 1406: 1395:. Retrieved 1393:. 2009-08-13 1381: 1370:. Retrieved 1368:. 2009-08-27 1356: 1345:. Retrieved 1331: 1320:. Retrieved 1309: 1298:. Retrieved 1287: 1276:. Retrieved 1262: 1251:. Retrieved 1241: 1230:. Retrieved 1219: 1208:. Retrieved 1197: 1186:. Retrieved 1156:. Retrieved 1128:. Retrieved 1099:. Retrieved 1069:. Retrieved 1055: 1044:. Retrieved 1030: 1019:. Retrieved 991:. Retrieved 963:. Retrieved 933:. Retrieved 926: 918: 907:. Retrieved 869:. Retrieved 792: 789:Availability 772: 705:typedef-name 704: 697:qualified-id 696: 690: 677: 667: 459: 453: 445: 386: 358: 347: 210: 196: 168: 136: 120: 95: 76: 58: 56:parameters. 28: 18: 1437:"std, Qstd" 701:decltype(e) 446:initializer 417:T&& 401:decltype(e) 375:decltype(e) 1446:2009-09-04 1422:2009-09-04 1397:2009-09-04 1372:2009-09-04 1347:2010-04-10 1322:2010-04-10 1300:2009-10-03 1278:2009-10-03 1253:2022-06-16 1232:2009-09-04 1210:2009-09-15 1188:2009-09-04 1158:2009-09-04 1130:2009-09-04 1101:2009-09-04 1071:2012-01-12 1046:2009-09-03 1021:2009-09-03 993:2009-09-03 965:2009-09-03 935:2009-09-04 931:. DevX.com 909:2015-08-28 871:2009-10-03 848:References 783:destructor 753:value_type 695:to form a 474:&& 110:expression 92:Motivation 84:operator, 42:expression 802:Microsoft 781:and in a 674:(a->x) 444:decltype( 355:Semantics 169:In 2002, 98:templates 1493:Category 927:decltype 830:Codegear 826:decltype 818:decltype 810:decltype 794:decltype 775:decltype 738:decltype 693:decltype 686:decltype 670:decltype 639:decltype 612:decltype 591:decltype 570:decltype 549:decltype 456:decltype 407:, where 367:decltype 349:decltype 307:decltype 265:template 207:decltype 199:decltype 192:decltype 156:typedefs 86:decltype 78:decltype 54:template 29:decltype 814:managed 421:prvalue 34:keyword 19:In the 840:, and 836:, the 720:vector 513:double 504:struct 413:xvalue 405:T& 397:lvalue 395:is an 362:sizeof 328:return 188:lvalue 176:typeof 152:typeof 133:typeof 128:sizeof 123:typeof 115:typeof 82:sizeof 67:typeof 40:of an 1341:(PDF) 1272:(PDF) 1182:(PDF) 1152:(PDF) 1095:(PDF) 903:(PDF) 842:Clang 798:C++11 709:C++0x 648:-> 621:-> 525:const 483:const 468:const 292:& 271:class 256:& 253:float 244:float 235:& 223:& 164:C++0x 46:C++11 32:is a 729:> 723:< 439:auto 304:> 280:auto 277:> 268:< 182:and 38:type 1499:C++ 832:'s 822:GCC 804:'s 726:int 713:ISO 579:()) 576:bar 558:()) 555:foo 546:(); 540:new 495:int 492:(); 489:bar 486:int 480:(); 477:foo 471:int 403:is 331:foo 313:foo 247:foo 232:int 226:foo 220:int 184:EDG 180:GCC 59:As 21:C++ 1495:: 1439:. 1389:. 1364:. 1167:^ 1139:^ 1123:. 1110:^ 1080:^ 1064:. 1039:. 1014:. 1002:^ 986:. 974:^ 958:. 944:^ 880:^ 855:^ 844:. 750::: 688:. 657:x5 654:)) 642:(( 630:x4 603:x3 582:x2 561:x1 522:}; 399:, 340:); 322:)) 262:); 241:); 215:: 194:. 141:, 26:, 1449:. 1425:. 1400:. 1375:. 1350:. 1325:. 1303:. 1281:. 1256:. 1235:. 1213:. 1191:. 1161:. 1133:. 1104:. 1074:. 1049:. 1024:. 996:. 968:. 938:. 929:" 912:. 874:. 765:; 762:0 759:= 756:i 747:) 744:v 741:( 735:; 732:v 660:; 651:x 645:a 633:; 627:) 624:x 618:a 615:( 606:; 600:) 597:i 594:( 585:; 573:( 564:; 552:( 543:A 537:= 534:a 531:* 528:A 519:; 516:x 510:{ 507:A 501:; 498:i 450:. 448:) 427:. 425:T 409:T 393:e 383:e 343:} 337:t 334:( 325:{ 319:t 316:( 310:( 301:− 298:) 295:t 289:T 286:( 274:T 259:f 250:( 238:i 229:(

Index

C++
programming language
keyword
type
expression
C++11
generic programming
template
generic programming
typeof
Bjarne Stroustrup
templates
generic programming
Standard Template Library
expression
typeof
sizeof
Dr. Dobb's Journal
Andrei Alexandrescu
Andrew Koenig
typedefs
Standard Template Library
C++0x
Bjarne Stroustrup
GCC
EDG
lvalue
forwarding function
trailing-return-type
sizeof

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.