Separating C++ template declaration and implementation

The following question was the inspiration for this short article:”Splitting a template and class into definition and declaration.“. In this question the asker asks, “I have the code below, which is all well and good but I’d like to move the definition of the setListener method to the cpp file, yet I seem to be having difficulty doing this as I get complaints about the template needing arguments?”.

Continue reading “Separating C++ template declaration and implementation”

Return Value Optimization

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. Advances in compiler optimizations have all but eliminated this concern thanks to a clever set of optimizations implemented by most modern compilers.

Continue reading “Return Value Optimization”

Static assertions in C++

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their way to become runtime problems are harder to detect and may go unchecked until such time that the code reaches a customer. The later the defect is identified the more costly it is in terms of time and money.

Continue reading “Static assertions in C++”

Determining if a C++ type is convertible to another at compile time

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generate a log or trace file for debugging purposes. The relationship of the types may have significance.

Continue reading “Determining if a C++ type is convertible to another at compile time”