Knowledge

Barton–Nackman trick

Source 📝

544:
possibility of removing friend name injection from the C++ programming language, Barton and Nackman's idiom was found to be the only reasonable use of that language rule. Eventually, the rules for argument-dependent lookup were adjusted to replace friend name injection by a less drastic mechanism, described above, that maintained the validity of Barton and Nackman's technique. It is worth noting that, as a consequence of this change, the expression
1190: 319:(and nonmember) functions (operator functions, in this case). At the time the idiom was introduced (1994), the C++ language did not define a partial ordering for overloaded function templates and, as a result, overloading function templates often resulted in ambiguities. For example, trying to capture a generic definition for 543:
The Barton–Nackman trick originally relied not on ADL but on a C++ feature called "friend name injection", in which an in-class declaration of a friend function made the function name visible in the immediately surrounding namespace scope (possibly the global scope). When investigating the
683: 1009: 487:
The Barton–Nackman trick, then, achieves the goal of providing a generic user-defined equality operator without having to deal with such ambiguities. The adjective
491:
in the idiom name refers to the fact that the provided in-class function definition is restricted (only applies) to specializations of the given class template.
1001: 1194: 676: 550:
is no longer valid, because qualified names aren't subject to ADL and friend declarations aren't found via ordinary lookup. Note, too, that the
903: 652: 923: 888: 563: 495: 49: 669: 1139: 554:
specifier is essential, even if the defined friend functions do not actually need to access nonpublic members of the befriending class.
908: 595: 1149: 1154: 1144: 1214: 1100: 857: 525: 938: 928: 918: 814: 779: 753: 498:(CRTP). As explained above, the Barton–Nackman trick is, instead, a distinct idiom (that relies on the CRTP). 981: 971: 839: 804: 862: 976: 847: 1169: 809: 794: 611: 898: 1159: 1039: 819: 1177: 946: 893: 648: 591: 29: 25: 824: 774: 708: 829: 788: 1080: 45: 1219: 1121: 991: 913: 644: 587: 956: 951: 769: 1208: 1064: 852: 584:
Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples
540:
were an incomplete template instance, ADL would trigger its complete instantiation.)
1024: 986: 961: 1095: 1059: 878: 1085: 1034: 1090: 1054: 1049: 1029: 799: 748: 743: 738: 733: 728: 723: 718: 1014: 532:. This lookup includes consideration of friend functions declared in 234:// Class value_type wants to have == and !=, so it derives from 784: 713: 703: 692: 1044: 1019: 966: 639:
Vandevoorde, David; Josuttis, Nicolai M.; Gregor, Douglas (2017).
48:
definition appearing in the base class template component of the
1105: 237:// equal_comparable with itself as argument (which is the CRTP). 57:// A class template to express an equality comparison interface. 665: 396:
would essentially be incompatible with another definition like
315:
is instantiated, the in-class friend definitions produce
24:
is a term coined by the C++ standardization committee (
661: 494:
The term is sometimes mistakenly used to refer to the
1168: 1130: 1114: 1073: 1000: 937: 871: 838: 762: 612:"An Alternative to Name Injection from Templates" 677: 32:introduced by John Barton and Lee Nackman as 8: 506:When the compiler encounters the expression 684: 670: 662: 44:The idiom is characterized by an in-class 582:Barton, John J.; Nackman, Lee R. (1994). 574: 904:Resource acquisition is initialization 16:Term for an idiom in the C++ language 7: 924:Substitution failure is not an error 889:Curiously recurring template pattern 564:Curiously recurring template pattern 536:and its base classes. (Note that if 496:curiously recurring template pattern 50:curiously recurring template pattern 1140:Comparison of programming languages 14: 641:C++ Templates: The Complete Guide 1189: 1188: 1150:Comparison of ALGOL 68 and C++ 1: 34:restricted template expansion 947:Comparison of C++ compilers 311:When a class template like 1236: 1155:Compatibility of C and C++ 1145:Comparison of Java and C++ 1186: 699: 526:argument-dependent lookup 929:Template metaprogramming 919:Special member functions 398: 325: 54: 982:Oracle Solaris Studio 28:WG21) to refer to an 1010:Comparison of C IDEs 884:Barton–Nackman trick 810:Operator overloading 795:Function overloading 547:::operator==(v1,v2) 21:Barton–Nackman trick 899:One Definition Rule 617:. 26 September 1995 1215:Programming idioms 1074:Superset languages 977:Intel C++ Compiler 785:Exception handling 1202: 1201: 1178:Bjarne Stroustrup 957:Borland Turbo C++ 894:Most vexing parse 830:Virtual functions 654:978-0-321-71412-1 26:ISO/IEC JTC1/SC22 1227: 1192: 1191: 1160:Criticism of C++ 840:Standard Library 789:Exception safety 780:Copy constructor 686: 679: 672: 663: 658: 627: 626: 624: 622: 616: 608: 602: 601: 579: 553: 539: 535: 531: 523: 519: 515: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 392: 389: 386: 383: 380: 377: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 322: 314: 313:equal_comparable 307: 304: 303:// to be defined 301: 298: 295: 292: 289: 286: 283: 280: 277: 274: 271: 268: 265: 262: 259: 256: 253: 252:equal_comparable 250: 247: 244: 241: 238: 235: 232: 229: 226: 223: 220: 217: 214: 211: 208: 205: 202: 199: 196: 193: 190: 187: 184: 181: 178: 175: 172: 169: 166: 163: 160: 157: 154: 151: 148: 145: 142: 139: 136: 133: 130: 127: 124: 121: 118: 115: 112: 109: 106: 103: 100: 97: 94: 91: 88: 85: 82: 79: 78:equal_comparable 76: 73: 70: 67: 64: 61: 58: 1235: 1234: 1230: 1229: 1228: 1226: 1225: 1224: 1205: 1204: 1203: 1198: 1182: 1164: 1133:other languages 1132: 1126: 1110: 1069: 996: 933: 867: 834: 758: 695: 690: 655: 638: 635: 633:Further reading 630: 620: 618: 614: 610: 609: 605: 598: 581: 580: 576: 572: 560: 551: 548: 537: 533: 529: 521: 517: 513: 510: 504: 485: 484: 481: 478: 475: 472: 469: 466: 463: 460: 457: 454: 451: 448: 445: 442: 439: 436: 433: 430: 427: 424: 421: 418: 415: 412: 409: 406: 403: 400: 394: 393: 390: 387: 384: 381: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 327: 320: 312: 309: 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: 218: 215: 212: 209: 206: 203: 200: 197: 194: 191: 188: 185: 182: 179: 176: 173: 170: 167: 164: 161: 158: 155: 152: 149: 146: 143: 140: 137: 134: 131: 128: 125: 122: 119: 116: 113: 110: 107: 104: 101: 98: 95: 92: 89: 86: 83: 80: 77: 74: 71: 68: 65: 62: 59: 56: 46:friend function 42: 17: 12: 11: 5: 1233: 1231: 1223: 1222: 1217: 1207: 1206: 1200: 1199: 1187: 1184: 1183: 1181: 1180: 1174: 1172: 1166: 1165: 1163: 1162: 1157: 1152: 1147: 1142: 1136: 1134: 1128: 1127: 1125: 1124: 1118: 1116: 1112: 1111: 1109: 1108: 1103: 1098: 1093: 1088: 1083: 1077: 1075: 1071: 1070: 1068: 1067: 1062: 1057: 1052: 1047: 1042: 1037: 1032: 1027: 1022: 1017: 1012: 1006: 1004: 998: 997: 995: 994: 989: 984: 979: 974: 969: 964: 959: 954: 949: 943: 941: 935: 934: 932: 931: 926: 921: 916: 911: 906: 901: 896: 891: 886: 881: 875: 873: 869: 868: 866: 865: 860: 855: 853:Smart pointers 850: 844: 842: 836: 835: 833: 832: 827: 822: 817: 812: 807: 805:new and delete 802: 800:Move semantics 797: 792: 782: 777: 772: 766: 764: 760: 759: 757: 756: 751: 746: 741: 736: 731: 726: 721: 716: 711: 706: 700: 697: 696: 691: 689: 688: 681: 674: 666: 660: 659: 653: 645:Addison-Wesley 643:(2 ed.). 634: 631: 629: 628: 603: 596: 588:Addison-Wesley 573: 571: 568: 567: 566: 559: 556: 546: 524:, it attempts 508: 503: 500: 399: 326: 55: 41: 38: 15: 13: 10: 9: 6: 4: 3: 2: 1232: 1221: 1218: 1216: 1213: 1212: 1210: 1197: 1196: 1185: 1179: 1176: 1175: 1173: 1171: 1167: 1161: 1158: 1156: 1153: 1151: 1148: 1146: 1143: 1141: 1138: 1137: 1135: 1129: 1123: 1120: 1119: 1117: 1113: 1107: 1104: 1102: 1099: 1097: 1094: 1092: 1089: 1087: 1084: 1082: 1081:Objective-C++ 1079: 1078: 1076: 1072: 1066: 1065:Visual Studio 1063: 1061: 1058: 1056: 1053: 1051: 1048: 1046: 1043: 1041: 1038: 1036: 1033: 1031: 1028: 1026: 1023: 1021: 1018: 1016: 1013: 1011: 1008: 1007: 1005: 1003: 999: 993: 990: 988: 985: 983: 980: 978: 975: 973: 970: 968: 965: 963: 960: 958: 955: 953: 950: 948: 945: 944: 942: 940: 936: 930: 927: 925: 922: 920: 917: 915: 912: 910: 909:Rule of three 907: 905: 902: 900: 897: 895: 892: 890: 887: 885: 882: 880: 877: 876: 874: 870: 864: 861: 859: 856: 854: 851: 849: 846: 845: 843: 841: 837: 831: 828: 826: 823: 821: 818: 816: 813: 811: 808: 806: 803: 801: 798: 796: 793: 790: 786: 783: 781: 778: 776: 773: 771: 768: 767: 765: 761: 755: 752: 750: 747: 745: 742: 740: 737: 735: 732: 730: 727: 725: 722: 720: 717: 715: 712: 710: 707: 705: 702: 701: 698: 694: 687: 682: 680: 675: 673: 668: 667: 664: 656: 650: 646: 642: 637: 636: 632: 613: 607: 604: 599: 597:0-201-53393-6 593: 589: 585: 578: 575: 569: 565: 562: 561: 557: 555: 545: 541: 527: 507: 501: 499: 497: 492: 490: 397: 324: 318: 53: 51: 47: 39: 37: 35: 31: 27: 23: 22: 1193: 1122:Embedded C++ 1025:Code::Blocks 992:Watcom C/C++ 883: 640: 619:. Retrieved 606: 583: 577: 549: 542: 520:are of type 511: 505: 502:How it works 493: 488: 486: 395: 316: 310: 43: 33: 20: 19: 18: 1131:Relative to 952:Borland C++ 848:I/O Streams 317:nontemplate 1209:Categories 1060:Qt Creator 987:Visual C++ 962:C++Builder 879:As-if rule 820:References 570:References 538:value_type 534:value_type 530:operator== 528:(ADL) for 522:value_type 489:restricted 321:operator== 282:value_type 258:value_type 243:value_type 1096:C++/WinRT 939:Compilers 825:Templates 815:Operators 754:Libraries 509:v1 == v2 479:/* ... */ 388:/* ... */ 40:The idiom 1195:Category 1170:Designer 1115:Dialects 1055:KDevelop 1050:NetBeans 1030:CodeLite 775:Concepts 763:Features 621:12 April 558:See also 419:operator 407:typename 401:template 346:operator 334:typename 328:template 276:equal_to 216:equal_to 162:operator 141:equal_to 90:operator 66:typename 60:template 52:(CRTP). 1086:C++/CLI 1040:Eclipse 1035:Dev-C++ 914:Slicing 863:Strings 770:Classes 709:Outline 249:private 1091:C++/CX 1015:Anjuta 651:  594:  552:friend 512:where 267:public 204:return 156:friend 132:return 84:friend 1045:Geany 1020:CLion 967:Clang 872:Ideas 749:C++26 744:C++23 739:C++20 734:C++17 729:C++14 724:C++11 719:C++03 714:C++98 615:(PDF) 467:& 464:const 452:Array 443:& 440:const 428:Array 376:& 373:const 361:& 358:const 297:const 288:& 285:const 240:class 192:& 189:const 177:& 174:const 120:& 117:const 105:& 102:const 75:class 30:idiom 1106:SYCL 1002:IDEs 649:ISBN 623:2005 592:ISBN 516:and 461:> 455:< 437:> 431:< 416:bool 413:> 404:< 343:bool 340:> 331:< 273:bool 261:> 255:< 159:bool 87:bool 72:> 63:< 1220:C++ 972:GCC 858:STL 704:C++ 693:C++ 323:as 291:rhs 1211:: 1101:Ch 647:. 590:. 586:. 518:v2 514:v1 422:== 349:== 306:}; 231:}; 225:); 165:!= 150:); 93:== 36:. 791:) 787:( 685:e 678:t 671:v 657:. 625:. 600:. 482:} 476:{ 473:) 470:b 458:T 449:, 446:a 434:T 425:( 410:T 391:} 385:{ 382:) 379:b 370:T 367:, 364:a 355:T 352:( 337:T 300:; 294:) 279:( 270:: 264:{ 246:: 228:} 222:b 219:( 213:. 210:a 207:! 201:{ 198:) 195:b 186:T 183:, 180:a 171:T 168:( 153:} 147:b 144:( 138:. 135:a 129:{ 126:) 123:b 114:T 111:, 108:a 99:T 96:( 81:{ 69:T

Index

ISO/IEC JTC1/SC22
idiom
friend function
curiously recurring template pattern
curiously recurring template pattern
argument-dependent lookup
Curiously recurring template pattern
Addison-Wesley
ISBN
0-201-53393-6
"An Alternative to Name Injection from Templates"
Addison-Wesley
ISBN
978-0-321-71412-1
v
t
e
C++
C++
Outline
C++98
C++03
C++11
C++14
C++17
C++20
C++23
C++26
Libraries
Classes

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