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:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

21 Responses to “Advanced C++ Idioms”

  1. Coplien’s book is one of excellent book on Advanced C++. The wiki link on More C++ Idioms is good collection (major contribution by Sumanth)

    http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms

    By Venki on May 8, 2011

  2. Use clear tape to attach duct to ends of T-shirt sleeves.

    By ??? ??? on Feb 15, 2025

  3. Thus, thus far we now have seen that the final functioning and well being of a country’s financial system affect the bank curiosity charges provided on deposits.

    By ?? ?? ?? on Feb 15, 2025

  4. Men and women appear to get the syndrome in equal numbers, although some estimates skew the number towards men.

    By ?????????? on Feb 15, 2025

  5. How to Start Intraday Trading?

    By ???? ?? ??? on Feb 16, 2025

  6. If you discover some form of mistake or simply something that you prefer to to change, the correction system will permit you to just do that, with one line of correction reminiscence.

    By ?? ??? on Feb 16, 2025

  7. They may invest in equity OR in debt structures in these start-up companies.

    By ?? ??? ?? on Feb 16, 2025

  8. The masked dancer misplaced their face-off and was in the underside four/three/two, but was not eliminated.

    By ?? ?? ??? ? on Feb 16, 2025

  9. Sharpen your pencil, put away your devices, and get ready to find out if you are the type who can mold the minds of the youth.

    By ???? ??? on Feb 16, 2025

  10. I’m genuinely impressed by the quality of this article. The depth of your research and the clarity with which you present your arguments are remarkable. Your ability to make complex topics understandable and engaging is truly commendable. Thank you for this excellent read. Regards, Pasang Iklan Properti Gratis

    By Pasang Iklan Sewa Hotel on Feb 17, 2025

  11. Allen, John (28 February 2011).

    By ?? 3???? on Feb 18, 2025

  12. Hemraz Bahorun. For public service.

    By Gmofhd??????? on Feb 18, 2025

  13. A joint undertaking with Mazda, this automobile was going to be the following Mustang.

    By ???????? 2015 on Feb 18, 2025

  14. Knowledge of native places, experience of serving clients for years for comparable things makes DMCs proficient.

    By ???? ?? ?? on Feb 18, 2025

  15. Some communities additionally supply grownup day care services, where the aging grownup can get a scorching meal and meet fellow seniors.

    By ??? ?? ?? on Feb 18, 2025

  16. For instance,? this bath is clearly massive, however even a small house will look larger if you retain the vanity, walls, and flooring in the identical light tones and clean textures.

    By ?????? ?? on Feb 18, 2025

  17. Hello there I am so grateful I found your site, I really
    found you by error, while I was searching on Bing for
    something else, Nonetheless I am here now and
    would just like to say kudos for a tremendous post and a all round entertaining blog (I also love
    the theme/design), I don’t have time to read it all at
    the minute but I have book-marked it and also added your RSS feeds, so when I have
    time I will be back to read more, Please do keep up the fantastic job.

    By cara trading forex on Feb 18, 2025

  18. Check out Rouen Cathedral, Morning Sun, Blue Harmony by Claude Monet.

    By ?????? on Feb 19, 2025

  19. Training that’s patterned to move from one set to another supplies the simplest learning mannequin.

    By ?????? on Feb 20, 2025

  20. Halter type necklines are perfect for the lariat fashion necklace.

    By ??????? on Feb 20, 2025

  21. Cloak of Deception describes the political background surrounding the Republic within the time interval before The Phantom Menace, as well as Darth Sidious’ plans to rule the galaxy, beginning with the blockade of Naboo.

    By ?????????? on Feb 20, 2025

Post a Comment