试图用C制作模板 [英] Trying to make templates in C
本文介绍了试图用C制作模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用宏在C语言中制作了一个通用向量。这个概念是否可行,或者我是否想去篝火单程旅行?
I made a generic vector in C using macros. Is the concept viable or do I get a one-way trip to the bonfire for even thinking about it?
#ifndef VECTOR_H
#define VECTOR_H
#define vector_at(vector, pos) ((vector).data[pos])
#define VECTOR_DEFINITION(type)\
typedef struct {\
size_t size;\
size_t capacity;\
type *data;\
} vector_ ## type ## _t;\
void vector_ ## type ## _reserve(vector_ ## type ## _t *vector, size_t size) {\
size_t capacity = 1;\
while (capacity < size) capacity *= 2;\
if (size == 0) capacity = 0;\
if (capacity != vector->capacity)\
{\
vector->capacity = capacity;\
if (size == 0) {\
free(vector->data);\
vector->data = NULL;\
} else {\
vector->data = realloc(vector->data, vector->capacity * sizeof(type));\
}\
}\
}\
void vector_ ## type ## _resize(vector_ ## type ## _t *vector, size_t size) {\
vector->size = size;\
vector_ ## type ## _reserve(vector, size);\
}\
void vector_ ## type ## _push_back(vector_ ## type ## _t *vector, type value) {\
if (vector->size >= vector->capacity) {\
if (vector->capacity == 0) vector->capacity = 1;\
else vector->capacity *= 2;\
vector->data = realloc(vector->data, vector->capacity * sizeof(type));\
}\
vector->data[vector->size++] = value;\
}\
type vector_ ## type ## _pop_back(vector_ ## type ## _t *vector) {\
return vector->data[--vector->size];\
}\
void vector_ ## type ## _init(vector_ ## type ## _t *vector, size_t size) {\
vector->size = size;\
vector->capacity = 0;\
vector->data = NULL;\
vector_ ## type ## _reserve(vector, size);\
}\
void vector_ ## type ## _destroy(vector_ ## type ## _t *vector) {\
free(vector->data);\
vector->size = 0;\
vector->capacity = 0;\
vector->data = NULL;\
}\
#endif
然后可以像这样使用代码:
The code can then be used like so:
#include <stdio.h>
#include <stdlib.h>
#include "vector.h"
typedef unsigned int uint;
typedef char* str;
VECTOR_DEFINITION(uint)
VECTOR_DEFINITION(str)
int main()
{
vector_uint_t vector;
vector_uint_init(&vector, 10);
for (unsigned int i = 0; i < vector.size; ++i)
vector.data[i] = i;
for (unsigned int i = 0; i < 10; ++i)
vector_uint_push_back(&vector, i);
for (unsigned int i = 0; i < vector.size; ++i)
printf("%d ", vector.data[i]);
printf("\n");
vector_uint_destroy(&vector);
vector_str_t sentence;
vector_str_init(&sentence, 0);
vector_str_push_back(&sentence, "Hello");
vector_str_push_back(&sentence, "World!");
vector_str_push_back(&sentence, "How");
vector_str_push_back(&sentence, "are");
vector_str_push_back(&sentence, "you?");
for (unsigned int i = 0; i < sentence.size; ++i)
printf("%s ", sentence.data[i]);
printf("\n");
vector_str_destroy(&sentence);
return 0;
}
并产生以下输出:
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
Hello World! How are you?
是否还会有更好的方法来实现类型安全的通用容器?
additionally, would there be a better way to implement generic containers with type safety?
推荐答案
评论者建议这个概念是可行的,因此,我认为这个问题已经得到解决。
Commenters have suggested that this concept is viable, and therefore I will consider this question answered.
有关更多信息,您可以查看以下链接:
For more information, you can take a look at the following links:
- pseudo-generics in C
- simulation of templates in C
- once_only pseudo-generic header in C
谢谢。
这篇关于试图用C制作模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文