1071:
417:. The main idea is to use commonality-variability analysis to divide the type into the fixed implementation and interface, the policy-based class, and the different policies. The trick is to know what goes into the main class, and what policies should one create. The article mentioned above gives the following answer: wherever we would need to make a possible limiting design decision, we should postpone that decision, we should delegate it to an appropriately named policy.
402:) base classes and implementations of interfaces by derived classes, in policy-based design the derived (host) class represents the interfaces and the base (policy) classes implement them. In the case of policies, the public inheritance does not represent an is-a relationship between the host and the policy classes. While this would traditionally be considered evidence of a design defect in OOP contexts, this doesn't apply in the context of the policy idiom.
390:. (Alternatives are for the host class to merely contain a member variable of each policy class type, or else to inherit the policy classes privately; however, inheriting the policy classes publicly has the major advantage that a policy class can add new methods, inherited by the instantiated host class and accessible to its users, which the host class itself need not even know about.) A notable feature of this aspect of the policy idiom is that, relative to
340:
situations requiring behaviors unforeseen by the library implementor. Even in cases where no more than one implementation of each policy will ever be used, decomposing a class into policies can aid the design process, by increasing modularity and highlighting exactly where orthogonal design decisions have been made.
1055:. Ongoing contributions by many people have improved the overall robustness and functionality of the library. Loki is not tied to the book anymore as it already has a lot of new components (e.g. StrongPtr, Printf, and Scopeguard). Loki inspired similar tools and functionality now also present in the
424:
It may be a delicate task to create a good set of policies, just the right number (e.g., the minimum necessary). The different customization points, which belong together, should go into one policy argument, such as storage policy, validation policy and so forth. Graphic designers are able to give a
40:
339:
number of different behavior combinations, resolved at compile time, and selected by mixing and matching the different supplied policy classes in the instantiation of the host class template. Additionally, by writing a custom implementation of a given policy, a policy-based library can be used in
343:
While assembling software components out of interchangeable modules is a far-fetched concept, policy-based design represents an innovation in a way it applies that concept at the (relatively low) level of defining the behavior of an individual class. Policy classes are similar to
182:. While Alexandrescu didn't invent the technique, he has popularized it among programmers. His book contains solutions to practical problems which C++ programmers may face. Several phrases from the book are now used within the C++ community as generic terms:
420:
Policy classes can contain implementation, type definitions and so forth. Basically, the designer of the main template class will define what the policy classes should provide, what customization points they need to implement.
459:
is a host class where it takes two policies, one for specifying how a message should be shown and the other for the actual message being printed. Note that the generic implementation is in
327:(or mostly orthogonal) aspect of the behavior of the instantiated host class. By supplying a host class combined with a set of different, canned implementations for each policy, a
367:, taking a set of behaviors represented by types as input, and returning as output a type representing the result of combining those behaviors into a functioning whole. (Unlike
1048:
1044:
986:
405:
A disadvantage of policies in their current incarnation is that the policy interface doesn't have a direct, explicit representation in
187:
379:
1158:
371:
metafunctions, however, the output is usually represented by the instantiated host class itself, rather than a nested output type.)
399:
273:
Previous examples of this design approach, based on parameterized generic code, include parametric modules (functors) of the
108:
425:
name to their policies, which represent concepts, and not those which represent operations or minor implementation details.
1076:
436:, which – at customization points – calls the appropriate functions of some of the policies.
17:
300:
1040:). Compiler vendors used Loki as a compatibility benchmark, further increasing the number of compliant compilers.
1213:
414:
1198:
455:, where the text to be printed and the method of printing it are decomposed using policies. In this example,
395:
263:
312:
296:
130:
391:
383:
345:
1218:
1178:
1043:
Maintenance and further development of Loki has been continued through an open-source community led by
967:
1109:
353:
274:
363:
or other facilities such as nested types. A policy-based host class can be thought of as a type of
1016:
Originally the library was only compatible with two of the most standard conforming C++ compilers (
982:
364:
357:
285:
255:
237:
179:
1159:
http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2011-Scott-Andrei-and-Herb-Ask-Us-Anything
1084:
1056:
1037:
429:
360:
320:
267:
324:
368:
250:
387:
332:
8:
1133:
971:
452:
328:
160:
50:
1183:
433:
336:
1024:): later efforts have made it usable with a wide array of compilers (including older
1010:
994:
278:
221:
115:
103:
440:
233:
141:
378:
idiom is that, usually (though it is not strictly necessary), the host class will
197:. The book has been republished and translated into several languages since 2001.
1006:
990:
398:
and derived class - whereas in OOP interfaces are traditionally represented by (
1021:
1002:
308:
164:
78:
1145:
428:
Policy-based design may incorporate other useful techniques. For example, the
1207:
1189:
1029:
998:
1052:
1025:
229:
194:
168:
1193:
1017:
410:
406:
193:
All of the code described in the book is freely available in his library
122:
39:
349:
463:
and therefore the code is unable to be compiled unless both policies (
293:
352:, a policy class will typically contain several related functions (
259:
167:. It has been regarded as "one of the most important C++ books" by
432:
can be reinterpreted for compile time, so that a main class has a
156:
Modern C++ Design: Generic
Programming and Design Patterns Applied
240:. It was first popularized in C++ by Andrei Alexandrescu with
1089:
1033:
964:
175:
116:
68:
142:
451:
Presented below is a simple (contrived) example of a C++
348:, but differ in that, rather than consisting of a single
254:, and it is currently closely associated with C++ and
413:, and must be documented separately and manually, in
186:(as opposed to C/C++ style), policy-based design and
1174:
1066:
878:// Does the same, but uses another language policy.
944:s by adding new classes with the member function
1205:
1157:C++ and Beyond 2011: "Ask Us Anything" session,
386:of) each of its policy classes using (public)
284:The central idiom in policy-based design is a
1199:Original source code from the book publisher
985:and implements several commonly used tools:
1146:Concepts: The Future of Generic Programming
394:, policies invert the relationship between
270:, which was not common before about 2003.
38:
409:, but rather is defined implicitly, via
303:with types selected by the user (called
981:The library makes extensive use of C++
14:
1206:
1134:Chapter 7. Abstract Types and Functors
200:
1110:"The Most Important C++ Books...Ever"
439:This will be achieved dynamically by
174:The book makes use of and explores a
1077:Free and open-source software portal
872:// Prints "Hello, World!".
24:
932:// Prints "Hallo Welt!".
220:for a design approach based on an
25:
1230:
1168:
446:
1069:
940:Designers can easily write more
955:
236:, and has connections with C++
1186:(sample chapter from the book)
1151:
1139:
1127:
1102:
281:for memory management policy.
228:. It has been described as a
13:
1:
1095:
178:programming technique called
7:
1062:
443:in future versions of C++.
392:object-oriented programming
27:Book by Andrei Alexandrescu
10:
1235:
246:Generic<Programming>
743:"Hello, World!"
210:policy-based class design
140:
128:
114:
102:
94:
84:
74:
64:
56:
46:
37:
983:template metaprogramming
473:
238:template metaprogramming
214:policy-based programming
180:template metaprogramming
1030:Borland C++ Builder 6.0
896:OutputPolicyWriteToCout
836:OutputPolicyWriteToCout
794:"Hallo Welt!"
623:OutputPolicyWriteToCout
430:template method pattern
356:), often combined with
292:class), taking several
163:, published in 2001 by
1214:2001 non-fiction books
1175:Alexandrescu's website
948:and take those as new
563:// Two policy methods.
311:a particular implicit
216:, is the term used in
1085:Boost (C++ libraries)
842:LanguagePolicyEnglish
707:LanguagePolicyEnglish
374:A key feature of the
288:template (called the
159:is a book written by
148:QA76.73.C153 A42 2001
1059:library collection.
974:as part of his book
902:LanguagePolicyGerman
758:LanguagePolicyGerman
388:multiple inheritance
382:from (make itself a
299:as input, which are
244:and with his column
1053:SourceForge project
972:Andrei Alexandrescu
545:// Behavior method.
453:hello world program
277:languages, and C++
251:C/C++ Users Journal
206:Policy-based design
201:Policy-based design
161:Andrei Alexandrescu
51:Andrei Alexandrescu
34:
18:Policy-based design
1177:(with book errata
434:skeleton algorithm
33:Modern C++ Design
32:
976:Modern C++ Design
963:is the name of a
851:HelloWorldEnglish
824:HelloWorldEnglish
258:as it requires a
242:Modern C++ Design
224:for C++ known as
218:Modern C++ Design
152:
151:
109:978-0-201-70431-0
16:(Redirected from
1226:
1162:
1155:
1149:
1143:
1137:
1131:
1125:
1124:
1122:
1120:
1106:
1079:
1074:
1073:
1072:
968:software library
951:
947:
943:
936:
933:
930:
927:
924:
921:
918:
915:
912:
911:HelloWorldGerman
909:
906:
903:
900:
897:
894:
891:
888:
885:
884:HelloWorldGerman
882:
879:
876:
873:
870:
867:
864:
861:
858:
855:
852:
849:
846:
843:
840:
837:
834:
831:
828:
825:
822:
819:
816:
813:
810:
807:
804:
801:
798:
795:
792:
789:
786:
783:
780:
777:
774:
771:
768:
765:
762:
759:
756:
753:
750:
747:
744:
741:
738:
735:
732:
729:
726:
723:
720:
717:
714:
711:
708:
705:
702:
699:
696:
693:
690:
687:
684:
681:
678:
675:
672:
669:
666:
663:
660:
657:
654:
651:
648:
645:
642:
639:
636:
633:
630:
627:
624:
621:
618:
615:
612:
609:
606:
603:
600:
597:
594:
591:
588:
585:
582:
579:
576:
573:
570:
567:
564:
561:
558:
555:
552:
549:
546:
543:
540:
537:
534:
531:
528:
525:
522:
519:
516:
513:
510:
507:
504:
501:
498:
495:
492:
489:
486:
483:
480:
479:<iostream>
477:
471:) are provided.
470:
466:
462:
234:strategy pattern
208:, also known as
144:
118:
86:Publication date
42:
35:
31:
21:
1234:
1233:
1229:
1228:
1227:
1225:
1224:
1223:
1219:Books about C++
1204:
1203:
1171:
1166:
1165:
1156:
1152:
1144:
1140:
1132:
1128:
1118:
1116:
1108:
1107:
1103:
1098:
1075:
1070:
1068:
1065:
1049:Richard Sposato
958:
949:
945:
941:
938:
937:
934:
931:
928:
925:
922:
919:
916:
913:
910:
907:
904:
901:
898:
895:
892:
889:
886:
883:
880:
877:
874:
871:
868:
865:
862:
859:
856:
853:
850:
847:
844:
841:
838:
835:
832:
829:
826:
823:
820:
817:
814:
811:
808:
805:
802:
799:
796:
793:
790:
787:
784:
781:
778:
775:
772:
769:
766:
763:
760:
757:
754:
751:
748:
745:
742:
739:
736:
733:
730:
727:
724:
721:
718:
715:
712:
709:
706:
703:
700:
697:
694:
691:
688:
685:
682:
679:
676:
673:
670:
667:
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:
468:
464:
460:
449:
335:can support an
232:variant of the
203:
133:
87:
28:
23:
22:
15:
12:
11:
5:
1232:
1222:
1221:
1216:
1202:
1201:
1196:
1187:
1184:Smart Pointers
1181:
1170:
1169:External links
1167:
1164:
1163:
1161:at 51:40-51:51
1150:
1148:stroustrup.com
1138:
1126:
1100:
1099:
1097:
1094:
1093:
1092:
1087:
1081:
1080:
1064:
1061:
1026:Visual C++ 6.0
1003:object factory
957:
954:
590:LanguagePolicy
533:LanguagePolicy
506:LanguagePolicy
485:<string>
474:
448:
447:Simple example
445:
305:policy classes
202:
199:
165:Addison-Wesley
150:
149:
146:
138:
137:
134:
129:
126:
125:
120:
112:
111:
106:
100:
99:
96:
92:
91:
88:
85:
82:
81:
79:Addison-Wesley
76:
72:
71:
66:
62:
61:
58:
54:
53:
48:
44:
43:
26:
9:
6:
4:
3:
2:
1231:
1220:
1217:
1215:
1212:
1211:
1209:
1200:
1197:
1195:
1191:
1188:
1185:
1182:
1179:
1176:
1173:
1172:
1160:
1154:
1147:
1142:
1135:
1130:
1115:
1111:
1105:
1101:
1091:
1088:
1086:
1083:
1082:
1078:
1067:
1060:
1058:
1054:
1050:
1046:
1041:
1039:
1035:
1031:
1027:
1023:
1019:
1014:
1012:
1008:
1004:
1000:
999:smart pointer
996:
992:
988:
984:
979:
977:
973:
969:
966:
962:
953:
472:
458:
454:
444:
442:
437:
435:
431:
426:
422:
418:
416:
412:
408:
403:
401:
397:
393:
389:
385:
381:
377:
372:
370:
366:
362:
359:
355:
351:
347:
341:
338:
334:
330:
326:
322:
321:encapsulating
318:
314:
310:
306:
302:
298:
295:
291:
287:
282:
280:
276:
271:
269:
265:
261:
257:
253:
252:
247:
243:
239:
235:
231:
227:
223:
219:
215:
211:
207:
198:
196:
191:
189:
185:
181:
177:
172:
170:
166:
162:
158:
157:
147:
145:
143:LC Class
139:
135:
132:
131:Dewey Decimal
127:
124:
121:
119:
113:
110:
107:
105:
101:
97:
93:
89:
83:
80:
77:
73:
70:
67:
63:
59:
55:
52:
49:
45:
41:
36:
30:
19:
1153:
1141:
1129:
1117:. Retrieved
1113:
1104:
1045:Peter Kümmel
1042:
1022:Comeau C/C++
1015:
1011:multimethods
980:
975:
960:
959:
956:Loki library
950:OutputPolicy
942:OutputPolicy
939:
920:hello_world2
914:hello_world2
875:// Example 2
818:// Example 1
605:OutputPolicy
524:OutputPolicy
497:OutputPolicy
456:
450:
438:
427:
423:
419:
404:
375:
373:
365:metafunction
342:
316:
309:implementing
304:
301:instantiated
289:
283:
272:
266:support for
262:with highly
249:
245:
241:
230:compile-time
225:
217:
213:
209:
205:
204:
192:
183:
173:
169:Scott Meyers
155:
154:
153:
29:
1194:SourceForge
1119:1 September
1018:CodeWarrior
970:written by
860:hello_world
854:hello_world
411:duck typing
384:child class
337:exponential
136:005.13/3 21
1208:Categories
1114:artima.com
1096:References
890:HelloWorld
830:HelloWorld
653:&&
515:HelloWorld
457:HelloWorld
396:base class
325:orthogonal
315:(called a
297:parameters
279:allocators
184:modern C++
1136:cam.ac.uk
995:singleton
764:protected
713:protected
629:protected
361:variables
346:callbacks
313:interface
268:templates
75:Publisher
1063:See also
987:typelist
683:<<
677:<<
503:typename
494:typename
488:template
482:#include
476:#include
441:concepts
415:comments
400:abstract
350:function
307:), each
260:compiler
226:policies
188:typelist
123:45129236
57:Language
1007:visitor
991:functor
779:Message
728:Message
680:message
656:message
596:Message
581:private
572:Message
530:private
521:private
469:Message
354:methods
329:library
319:), and
248:in the
65:Subject
60:English
791:return
776:string
740:return
725:string
650:string
539:public
380:derive
376:policy
333:module
317:policy
264:robust
98:323 pp
47:Author
1090:Mixin
1057:Boost
1051:as a
1034:Clang
946:Print
881:using
821:using
785:const
755:class
734:const
704:class
662:const
638:Print
620:class
611:Print
602:using
587:using
566:Print
557:const
512:class
465:Print
358:state
323:some
286:class
222:idiom
95:Pages
1190:Loki
1121:2023
1047:and
1036:and
1020:and
1009:and
961:Loki
905:>
893:<
845:>
833:<
809:main
692:endl
674:cout
635:void
575:());
548:void
509:>
491:<
467:and
407:code
294:type
290:host
195:Loki
117:OCLC
104:ISBN
90:2001
1192:on
1038:GCC
965:C++
952:s.
929:();
926:Run
869:();
866:Run
806:int
770:std
719:std
686:std
668:std
644:std
551:Run
461:Run
369:MPL
331:or
212:or
176:C++
69:C++
1210::
1112:.
1032:,
1028:,
1013:.
1005:,
1001:,
997:,
993:,
989:,
978:.
812:()
803:};
782:()
773:::
752:};
731:()
722:::
701:};
689:::
671:::
647:::
617:};
608:::
593:::
554:()
275:ML
190:.
171:.
1180:)
1123:.
935:}
923:.
917:;
908:;
899:,
887:=
863:.
857:;
848:;
839:,
827:=
815:{
800:}
797:;
788:{
767::
761:{
749:}
746:;
737:{
716::
710:{
698:}
695:;
665:{
659:)
641:(
632::
626:{
614:;
599:;
584::
578:}
569:(
560:{
542::
536:{
527:,
518::
500:,
256:D
20:)
Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.