views:

13

answers:

1

Hi,

in my unit test framework, for some of the messages ( which are simply POD structures ) I need a method to compare two such messages ( structs ) for equality of all fields. That is if for example I have a message:

struct SExampleMessage
{
    int someField;
    int someField2;
    char someField3[10];
};

I have a method that takes two pointers to SExampleMessage and returns true if all fields are equal in both structs:

bool compareExampleMessage(SExampleMessage* expectedMsg,
                           SExampleMessage* receivedMsg);

( The pointers of course could be void* and casted to the correct type )

My question is - is there any way to avoid writing this boilerplate code while staying typesafe and in the C++ realm? So instead of:

  1. One generic method that simply compares the binary content of the messages ( getting two void pointers and the size of the structure )

  2. Some external script that analyses the header file of the structure and generates the comparing method

is there any metaprogramming voodoo that makes similar thing? Possibly there isn't because then someone would easily implement C++ reflection with it, but it's worth a try :).

+1  A: 

If the structures are plain POD (no pointer internals) then you don't need to have a function and doing var A == var B of the same type is fine.

In C++0x they even relaxed the POD rules to allow classes with constructors and other things to remove this burden of boilerplate

http://www2.research.att.com/~bs/C++0xFAQ.html#PODs

David