‘c++’ Archive

Exponent operator overloading Saturday 2nd January 2010 1 Comment

In C++ you can’t create new operators to overload, you can only overload the operators that are already defined in the language. Often, an exponentiation operator is cited as an example of a new operator that you might want to have, so something like this isn’t possible. (Excuse the use of == with floating point, […]

Implementation of operator+ Monday 18th May 2009 2 Comments

Previously I’ve written about the performance implications of passing and returning values. It turns out that it needn’t be too expensive and that passing temporaries or initialising objects from function returns need not entail object copies. class Num { public: Num( const Num& ); Num& operator=( const Num& ); Num& operator+=( const Num& ); private: […]

Dependent bases, virtual functions, what does this call? Saturday 11th April 2009 5 Comments

Suppose we have an instance of MoreDerived, and we call the member function g on it. Which f gets called, and what syntax do we need in g to call the other f’s? // test.cc void f(); template< class T > class Base { public: virtual void f(); }; template< class T > class Derived […]

Pass by reference, return by value, etc Thursday 8th January 2009 2 Comments

Typically an assignment operator is declared like this. MyClass& operator=( const MyClass& ); If you’re implementing a copy and swap idiom for the sake of exception safety you might implement something like this. MyClass& operator=( const MyClass& other ) { MyClass copy( other ); Swap( copy ); return *this; } In this case we are […]

New things are always better? Wednesday 8th October 2008 3 Comments

Find the first non-digit in a string… size_t FirstNonDigit( const char* s ) { site_t i; for( i = 0; i < strlen( s ); ++i ) { if( !isdigit( s[i] ) ) break; } return i == strlen(s) ? (size_t)-1 : i; } Excellent... but old fashioned. We really want to use iterator style […]

Unit testing value classes with templates Tuesday 13th May 2008 Comments Off on Unit testing value classes with templates

Classes with value semantics are a very important, err, class of classes. Frequently used as the building blocks for other code, it is important that they are tested and behave as expected. The following function templates are designed to make testing the basic properties of value classes as simple as possible. They are designed to […]

Address Space Monitor 0.6 Release Thursday 17th April 2008 4 Comments

Address Space Monitor 0.6 has been released. In true ‘alpha’ quality style it has gained many features, while probably being no more stable than the previous release. The major new feature is virtual address space recording which enables you to record the address space usage of a program over time and later analyse the recording […]

The polymorphic equality problem Thursday 21st February 2008 2 Comments

For many situations the use of RTTI in C++ is unnecessary and it can often be an indicator of poor design. There are, however, some circumstances in which a dynamic_cast can make things cleaner and reduce dependencies. For many classes an equality operator is desirable, its implementation often passes through to its members in a […]

Quick reference guide for template declarations Tuesday 29th January 2008 1 Comment

Function templates by example Your basic template declaration. (No angle brackets after the template name.) template< class T, class U > T* just_template( U ); Declaration of explicit (full) specialization: template<> char* just_template< char, int >( int ); Explicit instantiation. (No angle brackets after the template keyword.) template const char* just_template< const char, double >( […]

Caution required with integer conversions Wednesday 2nd January 2008 Comments Off on Caution required with integer conversions

This applies to both C and C++, although the details are a little different in the more obscure corners. Consider this C++ snippet which is designed to position a streambuf somewhere a bit before its end: pos = sb->pubseekoff( -128 – thing.GetSize(), ios_base::end, ios_base::in ); How did this fail spectacularly? OK, here are the types. […]