C 中的枚举指针参数
我正在为一个项目编写一些测试功能。测试函数采用函数指针、预期值和测试值并检查它们:
int TestIntInIntOut(int (*func)(int val), int expected, int testvalue);
但是,我也在测试可以将枚举作为参数或退出值的函数。
假设如下:
typedef enum { Zero, One, Two } MyEnum;
有没有办法可以将整个枚举作为类型传递?对于任何枚举。例如:
int TestIntInEnumOut(?enum? (*func)(int val), ?enum? expected, int testvalue);
or
int TestEnumInIntOut(int (*func)(?enum? val), int expected, ?enum? testvalue);
(有点像 C# 中的泛型所允许的。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是指任何枚举,还是只是
MyEnum
?如果您只需要一个函数来支持
MyEnum
,您可以这样做:否则您可能无法在 C 中执行此操作,因为类型安全性很高。但枚举本质上与 int 相同,因此您可以将它们全部视为
int
类型。如果需要类型安全,请考虑为不同的枚举类型提供不同的整数值范围。Do you mean any enum, or just
MyEnum
?If you only need a function to support
MyEnum
, you could do: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.不是直接回答您的问题,但是正如您所说,您正在编写一些测试函数:您可能想看看 googletest 或 cppunit - 可以为您省去一些麻烦。
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.