Advanced C++ Idioms

C++ is a multi-paradigm programming language. This means that there is no single style that is always the right one to use on C++ programs. It all depends on the task at hand. For example, it is completely fine to use C++ only as a structured language, since it has all elements of C and a few more that make structured programming even easier in C++.

The main style of use of the C++ programming language is the classic object oriented style, since it is a style that is easier to reconcile with business practices that have been developed in the previous years.

However, not every project can be done with class object oriented methods. Sometimes it is more productive to use ideas developed in other programming traditions, such as symbolic, functional, and dynamic programming. C++ can also do this, but it needs a little additional thought from the part of the program designer.

The book Advanced C++ Programming Styles and Idioms discusses several idioms that can be successfully used with the C++ programming language.

Many of these idioms are not mainstream, and some of them may not be even acceptable in your organization. Despite this, these idioms could just as well  be the tool of choice for specific problem domains.

Dynamic Features in C++
Among these idioms, are techniques that have been used for a long time in dynamic languages such as Smalltalk and Lisp, such as dynamic dispatching, symbolic computing, and related techniques.

The author, James Coplien, describes how to implement object orientation based on exemplar objects, instead of class based objects. In examplar-based programming, one uses objects as the foundation for creating objects, instead of using classes — as is the normal practice in C++. One of the keys for doing this is to use factory methods to create new objects, instead of using the new keyword, which by definition requires knowledge about the specific class of the created object.

Another strategy is to use Envelope objects, which provide some of the functionality of symbols in a language such as Lisp. Using Envelope objects, one can have automatic memory management through reference counting, and a clean separation between a value and the label that contains it.

Coplien describes how to create more dynamic objects by avoid the classic hierarchy-based approach for classes. He proposes two main solutions for this problem: in the first solution, one creates a shallow hierarchy, where each class has all possible methods that could be implemented by subclasses. In this approach, each object is always crated using the interface of the parent class, and implements only the methods that are of their interest. Subclasses rely on polymorphism to receive the correct messages, and send an error whenever they don’t respond to a particular message.

This technique has the great advantage that clients don’t need to know exactly what class is being called. Also, since the differences between objects is now based only on what methods they respond to, the system is now based on generic methods, rather than on specific classes. Any class can override any method of the top class, which makes it easy to supply dynamic behavior.

The drawback of the previous approach however is that, unless we known all methods that are of interest, the system is subject to recompilation whenever a new method is added to the parent class. To avoid this problem, one can go one step further and create a dynamic dispatch system. In such a system, there is just one method to which an operation descriptor is passed, followed by a number of parameters. In this way, easy concrete subclass can add as many operations as necessary, without any change to the parent class. While such a system may not perform as quickly as a statically defined dispatch method, it is much more flexible, and allows the system to evolve without recompilation.

Finally, the books discuss how to load code dynamically, and therefore avoid linking. The approach works on most systems, however it is machine and compiler-dependent, so it should be used with care when portability is of importance for the system.

Further Reading

Check Advanced C++ Programming Styles and Idioms at Amazon.

Similar Posts:

Similar Posts

913 Comments

  1. ???? ????? ????????? ??-?????? ????????? ?? ????, ?? ??????? ?????? ? ??? ???????. ?? ????????? ?????, ??????? ???? ??? ??????, ? ???????, ???????? ??????? ?????? ??????????, ?? ??? ??? ????? ?????.
    ??? – ????? ??????

  2. Do you have a spam problem on this blog; I also am a blogger, and I
    was curious about your situation; many of us have developed some
    nice procedures and we are looking to trade methods with others, please shoot me an email if
    interested.

  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. Kunststof Brillenmonturen WaddinxveenMinervalaan, 2741
    PD Waddinxveenhttps://kunststofkozijnenwaddinxveen.nl/Kunststof brillenmonturen verschaffen veelzijdige keuzes
    in Nederland. Kunststof brillenmonturen ondersteunen een brede keuze
    in kleuren en vormen. Kunststof brillenmonturen helpen zowel herenbrillen als damesbrillen. Kunststof brillenmonturen bieden ook kinderbrillen en sportbrillen. Kunststof brillenmonturen bieden een assortiment van transparante en gekleurde monturen. Kunststof brillenmonturen faciliteren voor comfortabele pasvormen door lichtgewicht materiaal.
    Kunststof brillenmonturen bieden garantie op duurzaamheid.

    Kunststof brillenmonturen leveren multifocale glazen voor verschillende sterktes.
    Kunststof brillenmonturen faciliteren oogzorg met een oogmeting.
    Kunststof brillenmonturen helpen de optie om zonnebrillen te bestellen. Kunststof brillenmonturen verschaffen acetaat en plastic als
    materiaalkeuze. Kunststof brillenmonturen verschaffen transparante kunststof brillenglazen. Kunststof brillenmonturen verlenen titanium en metalen als alternatieve monturen.
    Kunststof brillenmonturen faciliteren voor een breed assortiment aan merken. Kunststof brillenmonturen verlenen aantrekkelijke prijzen voor
    elke klant. Kunststof brillenmonturen vereenvoudigen onderhoud en reparatie.
    Kunststof brillenmonturen verzorgen ook de keuze voor multifocale lenzen. Kunststof brillenmonturen bieden coatings voor
    extra bescherming. Kunststof brillenmonturen bieden leesbrillen voor optimaal
    zicht. Kunststof brillenmonturen bieden een collectie van moderne en klassieke
    stijlen. Kunststof brillenmonturen leveren ook op maat gemaakte oplossingen. Kunststof brillenmonturen vereenvoudigen een naadloze brillenervaring in Nederland.

    Kunststof brillenmonturen verlenen een perfecte balans tussen stijl en functionaliteit.
    Kunststof brillenmonturen verzorgen een innovatieve
    kijk op brillenmode.

  32. ? ??????-?????? ???????????? ?????? ???????????? ???????????? ? ????? ??? ????? ???????????. ???????? ??????????? ? ??????? ????????, ???? ??? ???? ?? ??????????? ??????? ??? ????????????? ? ????.
    ????????? ?????? – ????? ??? ????????? ??????

  33. ? ???? ????????? ???????????? ??????, ??????? ???? ? ????????????, ?? ?? ???????? ???????. ?? ????????????? ???????, ??????? ?????? ??????? ???????, ?? ??? ?? ?????? ???????? ?? ??? ??????? ???????.
    ??? – ?????????? ??? ????????

  34. ? ????????? ????????? ??????, ??????????, ?? ?? ???????? ??????. ?? ????????????? ???????, ??????? ?????? ??????? ?????????????, ?? ???????? ?? ??? ??????? ???????.
    ????????? ?????? – ???????????? ????

  35. ? ?????? ?????? ??????? ????????????? ????????? ???????? ? ?? ?????? ???????????? ????, ??????? ????? ??????? ???????. ?? ???????? ??????, ?? ???????? ???????? ????, ?? ??????????? ???? ????? ? ?????????.
    ????????? ?????? – https://rostov-narkologiya.ru

  36. ? ???? ????????? ???????????? ??????, ??????? ???? ? ????????????, ?? ?? ???????? ???????. ?? ????????????? ???????, ??????? ?????? ??????? ???????, ?? ??? ?? ?????? ???????? ?? ??? ??????? ???????.
    ??? – ?????????? ????? ?????

  37. ???? ??????? ?????????? ?????????? ?????????? ?????? ??????? ? ????????? ????????. ?? ?????????? ???????, ????? ????????, ?? ????????? ??????? ????????????? ?????????? ? ?????.
    ????????? ?????? – ???????????? ????

  38. ? ??????-?????? ???????????? ?????? ???????????? ???????????? ? ????? ??? ????? ???????????. ???????? ??????????? ? ??????? ????????, ???? ??? ???? ?? ??????????? ??????? ??? ????????????? ? ????.
    ????????? ?????? – ?????? ??? ?????? ???????

  39. Kunststof Brillenmonturen BeverwijkGooiland, 1948 RC Beverwijkhttps://kunststofkozijnen-beverwijk.nl/Kunststof brillenmonturen bieden veelzijdige
    opties in Nederland. Kunststof brillenmonturen faciliteren een brede keuze in kleuren en vormen. Kunststof brillenmonturen ondersteunen zowel herenbrillen als damesbrillen. Kunststof brillenmonturen bieden ook kinderbrillen en sportbrillen. Kunststof brillenmonturen verschaffen een assortiment van transparante en gekleurde monturen. Kunststof brillenmonturen zorgen voor
    comfortabele pasvormen door lichtgewicht materiaal.
    Kunststof brillenmonturen verschaffen garantie op duurzaamheid.
    Kunststof brillenmonturen leveren multifocale glazen voor verschillende sterktes.
    Kunststof brillenmonturen ondersteunen oogzorg met een oogmeting.
    Kunststof brillenmonturen helpen de optie om zonnebrillen te bestellen. Kunststof brillenmonturen verschaffen acetaat en plastic als materiaalkeuze.
    Kunststof brillenmonturen bieden transparante kunststof brillenglazen. Kunststof brillenmonturen verschaffen titanium en metalen als alternatieve monturen. Kunststof brillenmonturen ondersteunen voor een breed
    assortiment aan merken. Kunststof brillenmonturen verschaffen aantrekkelijke prijzen voor elke klant.

    Kunststof brillenmonturen faciliteren onderhoud
    en reparatie. Kunststof brillenmonturen verzorgen ook de keuze voor multifocale
    lenzen. Kunststof brillenmonturen verschaffen coatings
    voor extra bescherming. Kunststof brillenmonturen bieden leesbrillen voor optimaal zicht.
    Kunststof brillenmonturen bieden een collectie van moderne en klassieke
    stijlen. Kunststof brillenmonturen verschaffen ook op maat
    gemaakte oplossingen. Kunststof brillenmonturen ondersteunen een naadloze brillenervaring
    in Nederland. Kunststof brillenmonturen verlenen een perfecte balans tussen stijl en functionaliteit.
    Kunststof brillenmonturen verzorgen een innovatieve kijk op brillenmode.

  40. ???? ????? ?????????? ?????? ?????? ?? ????????, ??????? ? ??? ???? ????????. ?? ????????? ?????, ?? ??????? ???????? ????????, ? ??????? ? ??????? ?????????, ?? ??? ???????????? ?????.
    ????????? ?????? – ????? ???????????? ?????

  41. ???? ????? ?????????? ?????? ?????? ?? ????????, ??????? ? ??? ???? ????????. ?? ????????? ?????, ?? ??????? ???????? ????????, ? ??????? ? ??????? ?????????, ?? ??? ???????????? ?????.
    ????????? ?????? – ??????? ???????????? ??????????

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.