5

In C++ I want to make functions that when declared, gets automatically added to a map( or vector, doesn't really matter in this case) as a function pointer and is called later automatically. For example this would be useful if I am writing unit test framework and I just want users to declare each of their unit tests like this:

UNIT_TEST_FUNCTION(function_name){

// do something 

}

and instead something like this gets called

void function_name(){
  //do something
}
int temp = register_function("function_name", function_name); 

Where register_function() adds the user defined function in a map of function pointers for example. So basically, I need a mechanism that adds additional lines of code after a function definition, so that some action is performed automatically on the defined function. Is this possible using macros perhaps?

2
  • A macro can only generate a consecutive block of text, so no it can't do what you want. Commented Feb 28, 2013 at 18:08
  • if everybody has lambdas... Commented Feb 28, 2013 at 18:32

2 Answers 2

7

A macro can only generate a consecutive block of text. It can't lay things out the way you show in the question.

However if you're willing to rearrange a little, it can be done.

#define UNIT_TEST_FUNCTION(function_name) \
    void function_name(); // forward declaration \
    int temp##function_name = register_function(#function_name, function_name); \
    void function_name()
Sign up to request clarification or add additional context in comments.

1 Comment

I voted this up because it satisfies the question as is. I would also like to point out that it is much better to first attempt an object based approach, fallback on to a template metaprogramming approach, and if that doesn't work as a last resort use a macro. Macros are harder to maintain, debug, and do no sanity checking. I have found them useful in automating serialization of objects due to the string concatenation (##).
1

A single preprocessor macro can't do what you want because it can only generate a single, contiguous block of text. Preprocessor macros are stupid in the sense that they don't understand anything about the language -- hence the preprocessor in 'preprocessor macro'.

What you can do is use a pair of macros or tuple of macros to delimit the begin and end of your test case mapping, and a single macro for each individual test case. Something along these lines:

TEST_CASES_BEGIN

UNIT_TEST_FUNCTION(function_name){

// do something 

}

TEST_CASES_END

The Boost unit test facility uses a mechanism very similar to this. You might even (eventually) find this design to be a little more expressive than the design you are trying to achieve.

1 Comment

Can UNIT_TEST_FUNCTION be used to build up TEST_CASES_END, so that it will have a line for each call to UNIT_TEST_FUNCTION?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.