The C++ Standard is a pretty large and complex document; however, it is the bible as far as writing C++ code is concerned. The standard is full of exceptions that prove the rule, and this quiz demonstrates just one trivial example.
Month: December 2012
Comparing structs
This little quiz explores the pitfalls of trying to compare structs. Do you know the right way to check if two structs are the same?
Copying to stdout using STL
The STL (Standard Template Library) is a collection of generic algorithms and data structures. This little quiz demonstrates one of the many useful things one can achieve with just a few lines of code when utilizing the power of this library.
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”
Function pointers vs. Functors
Often, when implementing a feature, you won’t know how certain events should be handled at the point where they occur and you’d rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, what should it do now that it has this tag?
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.
Sealing a C++ Class
Unlike C#, C++ doesn’t have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism.
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.
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”
The Zen Of C++
Are you a C++ programmer who works with Python programmers? Are you fed up with them banging on about their “Python Philosophy”? Do you feel like beating them over the head with your compiler every time they witter on about using 4 space intending? Does the fact they continuously try to convince you enforced white space syntax is a good thing just make you want to hurl chunks? If so, what you need is “The Zen Of C++”!
