在 C 中使用模板函数的最短示例? [英] Shortest example to use templating function in C?

查看:33
本文介绍了在 C 中使用模板函数的最短示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何处理一个函数 echo_tpl,它可以采用 1 类型的 intstring 参数,以及打印出来?

How do I approach a function echo_tpl that can take 1 parameter of type int or string ,and print it out?

推荐答案

C 没有模板.我认为您能做的最好的事情是使用联合或让函数具有不同的名称.后一种具有不同名称的方法是这样做的准标准方法(例如 fabs fabsf fabsl,还有 大量使用 OpenGL 这也解释了 C 不能重载函数的事实)

C doesn't have templates. I think the best you could do is to use an union or to have the functions have different names. The latter way of having different names is the quasi-standard method of doing it (for instance fabs fabsf fabsl, also heavily used by OpenGL which also accounts for the fact C can't overload functions)

void echo_tpl_s(char const *string) { /* ... */ }
void echo_tpl_i(int number) { /* ... */ }

int main(void) {
  echo_tpl_s("Hello world");
  echo_tpl_i(42);
}

如果有很多通用代码,您可以决定将其分解为单独的函数

If there is a lot of common code, you may decide to factor it out in separate functions

void echo_tpl_s(char const *string) { 
  prepare_output_device();
  printf("%s", string);
  unprepare_output_device();
}

void echo_tpl_i(int number) { 
  prepare_output_device();
  printf("%d", number);
  unprepare_output_device();
}

<小时>

或者你可以采用union方式,这将使函数名称相等,但用元信息炸毁参数类型.


Or you can take the union way, which will have the function names be equal but instead blow up the parameter type with meta informations.

enum Type {
  Number,
  String
};

struct Value {
  enum Type type;
  union { 
    int number;
    char const *string;
  } u;
};

void echo_tpl(struct Value value) {
  switch(value.type) {
    case Number: printf("%d", value.u.number); break;
    case String: printf("%s", value.u.string); break;
  }
}

int main(void) {
  echo_tpl((struct Value) { 
    .type = String, 
    .u.string = "Hello world" 
  });
}

如果您想将值存储在某处然后执行打印函数而不关心传递给它的值类型,则联合方式特别适合.在 C89 中,您需要单独创建该值,因为它没有复合文字

The union way is particular well-suited if you want to store the value somewhere and then execute the print function without caring what value type you pass to it. In C89 you would need to create the value separately since it doesn't have compound literals

int main(void) {
  struct Value value;
  value.type = String;
  value.u.string = "Hello world";
  echo_tpl(value);
}

不过,为此创建函数是个好主意

It's a good idea to create functions for that, though

struct Value stringval(char const *string) {
  struct Value value;
  value.type = String;
  value.u.string = string;
  return value;  
}

struct Value numberval(int number) {
  struct Value value;
  value.type = Number;
  value.u.number = number;
  return value;  
}

int main(void) {
  echo_tpl(stringval("Hello world!"));
}

<小时>

某些编译器可能会提供用于编写​​此类内容的扩展.例如 Clang 提供了 C 中的函数重载.

void echo_tpl(int value) __attribute__((overloadable)) {
  printf("%d", value);
}

void echo_tpl(char const *value) __attribute__((overloadable)) {
  printf("%s", value);
}

这解决了函数调用端不依赖类型的问题.在定义方面,您仍然需要编写两次代码.这主要是因为(正如另一个答案所解释的)C 没有类型通用的输出函数.当然,如果您使用此功能,您的代码将变得不可移植.

This solves the call-side of the function not to depend on the type. On the definition side, you still have to write the code twice. That's mainly because (as another answer explains) C doesn't have type-generic output functions. Of course if you use this feature, your code becomes nonportable.

这篇关于在 C 中使用模板函数的最短示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆