Reputation: 16697
I'm writing some testing functions for a project. The test function takes a function pointer, an expected value and a test value and checks them:
int TestIntInIntOut(int (*func)(int val), int expected, int testvalue);
However, I'm also testing functions which can take an enum as a parameter or as an exit value.
Suppose the following:
typedef enum { Zero, One, Two } MyEnum;
Is there a way I can pass an entire enum as a type? For any enum. For example:
int TestIntInEnumOut(?enum? (*func)(int val), ?enum? expected, int testvalue);
or
int TestEnumInIntOut(int (*func)(?enum? val), int expected, ?enum? testvalue);
(Sort of like what a generic in C# would allow.)
Upvotes: 1
Views: 1513
Reputation: 19020
Not directly an answer to your question, however as you said you are writing some test functions: You might want to have a look at googletest or cppunit - could save you some trouble.
Upvotes: 0
Reputation: 5737
Do you mean any enum, or just MyEnum
?
If you only need a function to support MyEnum
, you could do:
int TestEnumInIntOut(int (*func)(MyEnum val), int expected, MyEnum testvalue);
Otherwise you probably can't do it in C, with much type safety. But an enum is essentially the same as an int, so you could just treat them all as int
types. If the type safety is necessary, consider providing different integer value ranges for different enum types.
Upvotes: 4