C - 使用宏的不同结构类型参数的方法重载
C - Method overloading with different struct type arguments using macro
我正在尝试在 C 中实现方法重载,最好使用宏,这样 header/library 就可以处理定义和声明,而不是将其交给用户。现在我已经阅读了这篇关于使用 _Generic
的 answer 但是问题是我的函数有 struct
类型。因此无法使用 _Generic
的 __typeof__
来评估它们的类型。不知道有没有什么办法。
这是我的头文件的样子(这是我们应该关心的)-
#pragma once
// Macros for generic like use of the data structure
// Macros for the stack type itself
/*
* Define stack with corresponding type
* Converts Stack(int) to intstack_t
*/
#define Stack(type) type stack_t
// Macros for functions
/*
* Define createStack function
* Converts createStack(int, 5) to create_intstack(5)
*/
#define createStack(type, capacity) create_ ##type ##stack(capacity)
/*
* Define destroyStack function
* Converts destroyStack(someIntStack) to destroy_intstack(someIntStack)
* Where someIntStack is a pointer to a variable of type intstack_t
*/
#define destroyStack(stack) _Generic(stack, intstack_t*: destroy_intstack, charstack_t*: destroy_charstack)(stack)
/*
* Define push function
* Converts push(someIntStack, data) to intstack_push(someIntStack, data)
* Where someIntStack is a pointer to a variable of type intstack_t
*/
#define push(stack, data) _Generic(stack, intstack_t*: intstack_push, charstack_t*: charstack_push)(stack, data)
// Stack structure definition(s)
// int stack definition
typedef struct IntegerStack {
int top;
unsigned capacity;
int* arr;
}intstack_t;
// char stack definition
typedef struct CharacterStack {
int top;
unsigned capacity;
char* arr;
}charstack_t;
//Stack functions
// int stack functions
intstack_t* create_intstack(int);
void destroy_intstack(intstack_t*);
void intstack_push(intstack_t*, int);
// char stack functions
charstack_t* create_charstack(int);
void destroy_charstack(charstack_t*);
void charstack_push(charstack_t*, char);
大部分函数声明(间接地,它们各自的宏)已被删除,因为它们本质上都具有相同的功能。我只关心所提供的 push
函数宏。其他宏实际上是为了展示我想要什么样的用例。显然,在 push
中使用 _Generic
的宏将不起作用,因为 intstack_t
或 charstack_t
不是基本类型。
目标是让用户能够使用 push(stack, data)
,其中堆栈可以是 intstack_t*
或 charstack_t*
类型的变量和语句 push(stack, data)
将分别转化为intstack_push(stack, data)
或charstack_push(stack, data)
。
_Generic
适用于除可变长度数组之外的任何完整类型。尽管您不能在 _Generic
调用中将不完整类型与值相关联,但指向不完整类型的指针没有问题。
所以您的 push
宏将正常工作,即使在 instack_t
和 charstack_t
不透明的上下文中使用也是如此。
coliru 样本:http://coliru.stacked-crooked.com/a/7d9b181af2429c5e
我正在尝试在 C 中实现方法重载,最好使用宏,这样 header/library 就可以处理定义和声明,而不是将其交给用户。现在我已经阅读了这篇关于使用 _Generic
的 answer 但是问题是我的函数有 struct
类型。因此无法使用 _Generic
的 __typeof__
来评估它们的类型。不知道有没有什么办法。
这是我的头文件的样子(这是我们应该关心的)-
#pragma once
// Macros for generic like use of the data structure
// Macros for the stack type itself
/*
* Define stack with corresponding type
* Converts Stack(int) to intstack_t
*/
#define Stack(type) type stack_t
// Macros for functions
/*
* Define createStack function
* Converts createStack(int, 5) to create_intstack(5)
*/
#define createStack(type, capacity) create_ ##type ##stack(capacity)
/*
* Define destroyStack function
* Converts destroyStack(someIntStack) to destroy_intstack(someIntStack)
* Where someIntStack is a pointer to a variable of type intstack_t
*/
#define destroyStack(stack) _Generic(stack, intstack_t*: destroy_intstack, charstack_t*: destroy_charstack)(stack)
/*
* Define push function
* Converts push(someIntStack, data) to intstack_push(someIntStack, data)
* Where someIntStack is a pointer to a variable of type intstack_t
*/
#define push(stack, data) _Generic(stack, intstack_t*: intstack_push, charstack_t*: charstack_push)(stack, data)
// Stack structure definition(s)
// int stack definition
typedef struct IntegerStack {
int top;
unsigned capacity;
int* arr;
}intstack_t;
// char stack definition
typedef struct CharacterStack {
int top;
unsigned capacity;
char* arr;
}charstack_t;
//Stack functions
// int stack functions
intstack_t* create_intstack(int);
void destroy_intstack(intstack_t*);
void intstack_push(intstack_t*, int);
// char stack functions
charstack_t* create_charstack(int);
void destroy_charstack(charstack_t*);
void charstack_push(charstack_t*, char);
大部分函数声明(间接地,它们各自的宏)已被删除,因为它们本质上都具有相同的功能。我只关心所提供的 push
函数宏。其他宏实际上是为了展示我想要什么样的用例。显然,在 push
中使用 _Generic
的宏将不起作用,因为 intstack_t
或 charstack_t
不是基本类型。
目标是让用户能够使用 push(stack, data)
,其中堆栈可以是 intstack_t*
或 charstack_t*
类型的变量和语句 push(stack, data)
将分别转化为intstack_push(stack, data)
或charstack_push(stack, data)
。
_Generic
适用于除可变长度数组之外的任何完整类型。尽管您不能在 _Generic
调用中将不完整类型与值相关联,但指向不完整类型的指针没有问题。
所以您的 push
宏将正常工作,即使在 instack_t
和 charstack_t
不透明的上下文中使用也是如此。
coliru 样本:http://coliru.stacked-crooked.com/a/7d9b181af2429c5e