Archive for the 'C\C++' Category

Getting Around Dynamic Casting To Achieve Better Design And Performance

Using dynamic casts to determine an execution path in your code is not the best to write your logic.

I realized this fact while looking at Google’s style guide for C++

Do not use RTTI, except in unit-tests. If you find yourself in need of writing code that behaves differently based on the class of an object, consider one of the alternatives to querying the type.

Virtual methods are the preferred way of executing different code paths depending on a specific subclass type. This puts the work within the object itself.

If the work belongs outside the object and instead in some processing code, consider a double-dispatch solution, such as the Visitor design pattern. This allows a facility outside the object itself to determine the type of class using the built-in type system.

If you think you truly cannot use those ideas, you may use RTTI. But think twice about it. :-) Then think twice again. Do not hand-implement an RTTI-like workaround. The arguments against RTTI apply just as much to workarounds like class hierarchies with type tags.

That was an eye-opener to me. I didn’t know what double-dispatch is all about. Turns out that it’s the heart of the Visitor’s Pattern (although the example at dofactory.com is not ideal because they do not provide overloads for the visit() function and they cast all types to the base).

 

Double-Dispatch is the ability to dynamically select a method according to the run-time type of the caller (single dispatch) and the run-time type of the argument as well.

 

To close out, remember before you use dynamic casting in your code to think if there is a better way to achieve the same results using function overloads, virtual functions or double dispatch.

Platform-Independent Bitwise Rotation function

A simple macro to rotate the bits of an unsigned 32-bit value. using shift-left and shift-right we can achieve rotate-left and rotate-right.

#define rotlFixed(x,n) (((x) << (n)) | ((x) >> (32 - (n))))

#define rotrFixed(x,n) (((x) >> (n)) | ((x) << (32 - (n))))

The macro above can be trivially converted to a function and used in C# for example.

Function Pointers

Declaring function pointers in C\C++ has a somewhat strange syntax, specially when you want to specify a function pointer as the return type of another function.
so here is a brief explanation:

To declare a pointer to an int called pNumber:

int *pNumber;

To declare a pointer to a function called pFunc (that has a string parameter and returns an int):

int (* pFunc)(string);

in red is the variable name, in purple is the type.

To declare a function that returns a pointer to a function:

int (* GetFuncPointer(void) )(string);

in red is a normal function declaration, in purple is the return type of that function.
S o basically int(*)(string) is the type of the function pointer.

If we want to declare a function that takes a function pointer as a parameter then we would write something like this:

void RegisterCallBack( int(*)(string) ); /* unnamed parameter */

or

void RegisterCallBack( int(*myCallback)(string) ); /* named parameter */

A prettier way to use function pointers is to use typedef to declare an alias. The typedef syntax is exactly the same as declaring a regular variable (or a function pointer).

typedef int(*FunctionPointer)(string);

now we can use FunctionPointer like this:

FunctionPointer GetFuncPointer(void);

Click For More Information…

Mr. Compiler, would you please inline this function?

I’ve always read that marking a function inline doesn’t guarantee that the compiler will actually inline it, but it’s merely a hint to the compiler to “try its best” to do it. Which have always raised the question in my head, how do I know if Mr. Compiler agreed to inline my function? Do I have to look at the generated assembly code? There has to be an easier way!

So I invested 15 minutes researching that issue and found out that Mr. Compiler is friendly enough to let you know if it was not possible to fulfill your request to inline a certain function by issuing a warning.

For Microsoft’s VC8 compiler warning C4710 is turned off by default, you can turn it on by doing any of the following (depends on your choice):

  1. #pragma warning (default : 4710)
  2. #pragma warning (custom_warning_level : 4710)

For GCC the -Winline switch can be used to achieve the same thing.

The typename Keyword

Indicates that the name following represents a parameterized type placeholder that will be replaced by a user-specified actual type.

consider the following example:
template<class T> class A
{
T::x(y); // ambiguous: calling a function or constructing an object?
// without ‘typename’ this will be interpreted as a function call
typedef char C;
A::C d;
}

The statement A::C d; is ill-formed. The class A also refers to A<T> and thus depends on a template parameter. You must add the keyword typename to the beginning of this declaration:

typename A::C d;

You can also use the keyword typename in place of the keyword class in template parameter declarations.

typename was introduced after the keyword class to solve the problems shown above, but both keywords are equivalent in declaring template parameters.