The C++ Role in Industry

A hot debate has been maintained for several years about the advantages or disadvantages of C++ in the practice of programming. C++ has certainly gained a lot of strength in the market, especially since the new standard (C++14) was approved and became supported by most compiler vendors. On the other hand, C++ still receives lots of critics for being a language that is not very accessible to new developers and with lots of rough edges that make it difficult to create code free of known “pitfalls”.

While some of these critics are well deserved, the language has taken a lot of steps in the right direction by providing safer mechanisms for initializing objects, for example. They have also done a good job to reduce verbosity in the type system, though the use of automatic type detection.

Another group of programmers have frequently advocated for the use plain C as the best way to fight the problems in the C++ language. This has been epitomized by the debate between Linus Torvalds and some developers that wished to introduce C++ code into the Linux kernel. This can be seen as a both a movement towards the abandonment of C++ altogether, or just as a way of avoiding the more complex features and using C++ as a “better C”.

Industry versus Single Users

While the pros and cons of C++ provide for a multifaceted debate,  I would like to remark on the differences between C and C ++ in industrial applications. This is a important distinction to make, because the needs of industry are frequently at odds with other uses of the language, such as in an academic scenario, or as a vehicle for personal programming projects.

C++ shines on its extensive support for higher level constructs, most notably classes and templates. At the same time, C language users have been more concerned about the high performance and transparency provided by C syntax. This just reflects the different priorities of these groups of users.

Industrial use of a language normally emphasizes the collaborative aspects of programming. In a way, it is much easier to have dozens of programmers working in a project when each programmer is responsible for a single class, which is contained in its own file with clear boundaries to the rest of the application. While it could be possible to replicate this kind of division of labor with C, it is much more complicated to design a set of programming standards that give the same separation between modules as provided by C++.

At the same time, single programmers see the verbosity of C++ as a deficiency, since it requires a lot of extra work to provide something that is not necessary for their needs. For programmers that work mostly alone, C is the most direct way to design an application because it doesn’t impose verbose boundaries between the different parts of the application. The individual programmer will in most cases have the whole project in his or her own mind, and artificial boundaries will only slow down the process. In a industrial setting, however, different programmers are assigned to maintain their own interfaces, making the whole process much easier to manage.

Moreover, programming as done within companies is every day more reliant on connecting to external libraries and frameworks. This is another situation in which C++, with its standard (even if cumbersome) class model provides a definite advantage over C. Suddenly programmers are able to create and manage objects provided by other groups or companies, using the same mechanisms for creating, and releasing objects.  C programmers normally have a more difficult time because each library has it own mechanics for managing data objects, which may not match properly with the style of the current code.

In summary, the needs of individual users are frequently at odds with the needs of corporations. Given these differences, it is not surprising that some users prefer C to C++, while C++ has become so successful in industry. This is also a reason why many open source projects prefer C instead of C++. While collaborative in nature, open source developers are more used to work alone in their projects, and combining their contributions into mainstream. This is a very different mindset from programming as performed in companies, and in more corporate-like projects such as Mozilla for example.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Avoiding Lengthy C++ Template Instantiations

C++ templates are a powerful mechanism that can be used to create generic code. With templates, it is also possible to remove undesirable code duplication, since the same code can then be applied to data of different types.

On the flip-side, however, templates can also create problems due to the potential they have to slow down compilation times. Because all the code in a template is generally available to the compiler when processing translation units, it is difficult to provide separate compilation for templates. An example of library that is victim of this behavior is boost, where typically all the functionality is included in header files. These header files are then included each time the library is referenced in an implementation file, resulting in long build times.

Despite these shortcomings, in some situations it is possible to reduce the amount of work done by the compiler in behalf of templates. This article shows a simple technique that can be used to achieve faster template compilation speeds in the particular case in which desired instantiations are known ahead of time.

Pre-Instantiating Templates

Certain templates are known to be used in only a reduced number of cases. For example, consider a numeric library that creates code for different floating point types. Each class in the library can be instantiated with a particular floating point type, such as double, long double, or float. Consider for instance the following definition:

// file mathop.h
template <class T>
class MathOperations {
public:
 static T squared(T value) {
 return value * value;
 }
// ...
};

This class can be used in the following way:

#include <mathop.h>
MathOperations<double> mathOps;
double value = 2.5;
cout << "result: " << mathOps.squared(2.5) << endl;

Unfortunately, because the class MathOperations is a template class, we have to include its complete definition as part of the header file, where it can be found by the compiler whenever the class is instantiated.

A possible way to reduce the size of the header file is to pre-instantiate the template for the types that we known in advance.

The first step is to remove the implementation from the header file. This is clearly possible, since you can implement class member functions outside the class declaration (if the class is a template or not). Then, you need to add the implementation to a separate source file. Once this step is done, client code will be able to use the template class interface, but will not be able to generate code. Therefore, for this to work, you need to instantiate the templates on the implementation file.

// file mathop.h
template <class T>
class MathOperations {
public:
 static T squared(T value);
 // ...
};
// file mathop.cpp
// template member function definition
template <class T>
T MathOperations<T>::squared(T value) {
 return value * value;
}
void instantiateMathOps() {
 double d = MathOperations<double>::squared(2.0);
 float f = MathOperations<float>::squared(2.0);
 int i = MathOperations<int>::squared(2);
 long l = MathOperations<long>::squared(2);
 char c = MathOperations<char>::squared(2);
}

In the example above, I chose to instantiate five versions of the original template for numeric types. The main limitation of this technique, as I mentioned above, is that your clients will not be able to generate templates for the additional types they may want to use. However, in a few situations you may really want to restrict how these templates are used, and the technique above works as desired.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Practical C++ Financial Programming

51SuC8CKLLL._UY250_My new book, Practical C++ Financial Programming, has just been released by Apress: http://amzn.to/1C8ekwg.
Practical C++ Financial Programming is a hands-on book for programmers wanting to apply C++ to programming problems in the financial industry. The book explains those aspects of the language that are more frequently used in writing financial software, including the STL, templates, and various numerical libraries. The book also describes many of the important problems in financial engineering that are part of the day-to-day work of financial programmers in large investment banks and hedge funds.

Focus is on providing working solutions for common programming problems. Examples are plentiful and provide value in the form of ready-to-use solutions that you can immediately apply in your day-to-day work. You’ll learn to design efficient, numerical classes for use in finance, as well as to use those classes provided by Boost and other libraries. You’ll see examples of matrix manipulations, curve fitting, histogram generation, numerical integration, and differential equation analysis, and you’ll learn how all these techniques can be applied to some of the most common areas of financial software development. These areas include performance price forecasting, optimizing investment portfolios, and more. The book style is quick and to-the-point, delivering a refreshing view of what one needs to master in order to thrive as a C++ programmer in the financial industry.

  • Covers aspects of C++ especially relevant to financial programming.
  • Provides working solutions to commonly-encountered problems in finance.
  • Delivers in a refreshing and easy style with a strong focus on the practical.

What you’ll learn

  • Understand the fundamental problem types in the financial market.
  • Design algorithms to solve financial programming problems.
  • Extend C++ through Python extensions and LUA modules.
  • Employ third-party numeric libraries such as those from Boost.
  • Properly engage key C++ features such as templates and exception handling.
  • Benefit from new features in C++14, such as auto variables and closures.

Who this book is for

Practical C++ Financial Programming is for professionals or advanced students who have interest in learning C++ financial programming, especially in preparation for a professional career. Readers should have a working-knowledge of programming in C, C++, or some other C-like language. The book is also useful to current practitioners at financial institutions as a ready-reference to common development problems and techniques.

Table of Contents

  1. The Fixed-Income Market
  2. The Equities Market
  3. C++ Programming Techniques in Finance
  4. Common Libraries for Financial Code
  5. Designing Numerical Classes
  6. Plotting Financial Data
  7. Linear Algebra
  8. Interpolation
  9. Calculating Roots of Equations
  10. Numerical Integration
  11. Solving Partial Differential Equations
  12. Algorithm Optimization
  13. Portfolio Optimization
  14. Monte Carlo Methods for Equity markets
  15. Extending Financial Libraries
  16. C++ with R and Octave
  17. Multithreading
  18. Appendix A: C++14 Features

Source Code

You can also access source code for the book using its git repository.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter