C中的泛型 [英] Generics in C

查看:51
本文介绍了C中的泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章是关于如何在C环境中获取通用模块的。

的目标是接近Ada仿制药。使用C ++不是一种选择。它应该都是在一个简单的C编译器中运行。


当我想到一个通用模块时,可以使用它。

完全一样。通用的H文件和/或C文件,不必为了使用它而全部编辑。如果必须在1 / b
行中进行更改,它可能仍然具有很好的可重用性属性,但是从我的意义上来说,它是b / b不是通用的。


我将通过提供

5文件作为例子来提出一种做泛型的方法。我非常希望评论如何简化或扩展内容,但仍然只使用C.


好​​的,我们走了。可以说,我们希望通过重复乘法来实现通用功率。模块的用户必须提供

类型,以及在该类型上执行乘法的函数。


通用模块由GenPower.h表示和GenPower.c。

实例化模块是My.h和My.c,然后我们有一个测试程序。

测试程序将计算3 ^ 5 = 3 * 3 * 3 * 3 * 3 = 243.它编译为

并在VC6中运行。


运行测试程序将此打印到stdout:

结果= 243


让我知道你对这个想法的看法。好?坏?老了?

复杂吗? (在这种情况下,我同意,但我认为这是值得的。)

//文件GENPOWER.H

//正式通用参数:

// INSTANCE实例模块的名称。

// ATYPE实例类型。

//(INSTANCE)_Multiply类型的乘法函数。

//(详见下面的原型。)

//

// void INSTANCE_Multiply(ATYPE *,ATYPE);

//在您的模块中实现,否则在链接时将错过

//

//

//详细信息正式参数函数:

#define MODULE_Multiply INSTANCE()## _ Multiply

void MODULE_Multiply(ATYPE * first_operand_and_result,ATYPE

other_operand); < br $>
//

//包装:(这个模块一般提供什么)

#define MODULE_Power INSTANCE()## _ Power

void MODULE_Power(ATYPE * result,int exponent);

//

//结束o f文件

//文件GENPOWER.C

//这只是#include -ing,不要链接这个模块。

#包括GenPower.h

void MODULE_Power(ATYPE * result,int exponent)

{//指数必须至少为1.

ATYPE a;

int i;

a = * result;

for(i = 1; I<指数; i ++){

MODULE_Multiply(结果,a);

}

}

//文件MY.H

typedef unsigned long MYTYPE;

//好了,现在我们已经声明了我们的类型,让我们通过实例化来获得幂函数

//通用电源模块:

#define INSTANCE()我的

#define ATYPE MYTYPE

#include" GenPower.H"

//

无效MY_Multiply(MYTYPE *,MYTYPE);

//文件结束

//文件MY.C

#include" my.h"

#include" genpower.c"

//是的,我们包括C文件,不是H文件。

//这样我们实际上就可以在这个模块中获得函数的实现

// void MYTYPE_Power(MYTYPE *,int)。

//当然,我们还必须提供乘法功能:

void MY_Multiply(MYTYPE * first_operand_and_result,MYTYPE

other_operand)

{

//做多事licatish。

(* first_operand_and_result)* = other_operand;

}

//文件结束

//测试程序

#include< stdio.h>

#include" my.h"

int main(int argc,char * argv [])

{

MYTYPE a;

a = 3;

MY_Power(& a,5 );

printf(权力结果=%d \ n,(int)a);

返回0;

}

//文件结束

解决方案

ttl_id ... @ yahoo.com写道:


这篇文章是关于如何在C环境中获取通用模块的。

的目标是接近Ada仿制药。使用C ++不是一种选择。它应该都是在一个简单的C编译器中运行。


当我想到一个通用模块时,可以使用它。

完全一样。通用的H文件和/或C文件,不必为了使用它而全部编辑。如果必须在1 / b
行中进行更改,它可能仍然具有很好的可重用性属性,但是从我的意义上来说,它是b / b不是通用的。


我将通过提供

5文件作为例子来提出一种做泛型的方法。我非常希望评论如何简化或扩展内容,但仍然只使用C.


好​​的,我们走了。可以说,我们希望通过重复乘法来实现通用功率。模块的用户必须提供

类型,以及在该类型上执行乘法的函数。



在我看来,这违背了通用编程的目的。


< snip>


santosh写道:


ttl_id ... @ yahoo.com写道:


这篇文章是关于如何在C环境中获取通用模块的。

的目标是接近Ada仿制药。使用C ++不是一种选择。它应该都是在一个简单的C编译器中运行。


当我想到一个通用模块时,可以使用它。

完全一样。通用的H文件和/或C文件,不必为了使用它而全部编辑。如果必须在1 / b
行中进行更改,它可能仍然具有很好的可重用性属性,但是从我的意义上来说,它是b / b不是通用的。


我将通过提供

5文件作为例子来提出一种做泛型的方法。我非常希望评论如何简化或扩展内容,但仍然只使用C.


好​​的,我们走了。可以说,我们希望通过重复乘法来实现通用功率。模块的用户必须提供

类型,以及在该类型上执行乘法的函数。



在我看来,这违背了通用编程的目的。



泛型的目的是编写一次算法,并允许它们适用于不同类型的
。 OP的提议并不像它可能是一般的,但它是* *通用编程的一种形式。我在下面的例子中给出了另一个更通用的方法:

http://www.pobox.com/~qed/ll.zip
http://www.pobox.com/~qed/gstack.zip


- -

Paul Hsieh
http:// www .pobox.com / ~qed /
http://bstring.sf .net /


" santosh" < sa ********* @ gmail.comwrites:


ttl_id ... @ yahoo.com写道:


[...]


>好的,我们走了。可以说,我们希望通过重复乘法来实现通用功能。模块的用户必须提供
类型,以及在该类型上执行乘法的函数。



在我看来,这违背了通用编程的目的。


< snip>



我不同意。如果通用模块可以在给定类型的情况下推断出适当的乘法运算符(< OT>为Ada

泛型可以做< / OT>),那将是很好的,但是在C中很难或不可能。


另请注意,它并不只是*要求*您提供

乘法功能,*允许*你这样做。如果你提供了一个执行加法的

函数,那么泛型将执行

乘法。它提供了一种通用的方法来指定重复应用一些

任意操作。从某种意义上说,这比仅仅提供电源功能更通用了。


(我没有仔细阅读代码来验证这将是实际工作的。$

-

基思汤普森*** @ mib.org> ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


This post is about how to get generic modules in a C environment. The
aim is to get close to Ada generics. Using C++ is not an option. It
should all run in a simple C compiler.

When I think of a generic module, it is something that can be used
exactly as it is. The generic H-file and/or C-file, should not have to
be edited AT ALL in order to use it. If it has to be changed in just 1
line, it could possibly still have great reusability properties, but it
is not generic in the sense I mean.

I am going to give a suggestion for a way to do generics, by presenting
5 files as an example. I would very much like comments on how things
could be simplified or extended, but still using only C.

Ok, here we go. Lets say, we want to implement generic powers, by
repeating multiplication. The user of the module have to provide the
type, and a function that performs multiplication on that type.

The generic module is represented by GenPower.h and GenPower.c. The
instantiation module is My.h and My.c, and then we have a test program.
The test program will calculate 3^5 = 3*3*3*3*3 = 243. It is compiled
and run in VC6.

Running the test program prints this to stdout:
Result of power = 243

Let me know what you think of the idea. Good? Bad? Old?
Complicated? (In that case I agree, but I think it is worth it.)
// File GENPOWER.H
// Formal Generic Parameters:
// INSTANCE The name of the instance module.
// ATYPE The instance type.
// (INSTANCE)_Multiply Multiplication function for the type.
// (See prototype below for details.)
//
// void INSTANCE_Multiply(ATYPE *, ATYPE);
// is implemented in your module, otherwise it will be
// missed at link time.
//
// Details of formal parameter functions:
#define MODULE_Multiply INSTANCE()##_Multiply
void MODULE_Multiply(ATYPE *first_operand_and_result, ATYPE
other_operand);
//
// Package: (What this module provides generically)
#define MODULE_Power INSTANCE()##_Power
void MODULE_Power (ATYPE *result, int exponent);
//
// End of file
// File GENPOWER.C
// This is just for #include -ing, Do not link this module.
#include "GenPower.h"
void MODULE_Power (ATYPE *result, int exponent)
{ // Exponent must be at least 1.
ATYPE a;
int i;
a = *result;
for (i=1; i<exponent; i++) {
MODULE_Multiply (result, a);
}
}
// File MY.H
typedef unsigned long MYTYPE;
// Ok, now we have declared our type, lets get the power function
// by instantiating the generic power module:
#define INSTANCE() MY
#define ATYPE MYTYPE
#include "GenPower.H"
//
void MY_Multiply(MYTYPE *, MYTYPE);
// End of file
// File MY.C
#include "my.h"
#include "genpower.c"
// Yes we are including the C file, not the H file.
// That way we actually get an implementation of the function
// void MYTYPE_Power(MYTYPE *, int) right here in this module.
// Of course, we also have to provide the multiplication function:
void MY_Multiply(MYTYPE *first_operand_and_result, MYTYPE
other_operand)
{
// Do something multiplicatish.
(*first_operand_and_result) *= other_operand;
}
// End of file
// Test program
#include <stdio.h>
#include "my.h"
int main(int argc, char* argv[])
{
MYTYPE a;
a = 3;
MY_Power(&a, 5);
printf ("Result of power = %d\n", (int) a);
return 0;
}
// End of file

解决方案

ttl_id...@yahoo.com wrote:

This post is about how to get generic modules in a C environment. The
aim is to get close to Ada generics. Using C++ is not an option. It
should all run in a simple C compiler.

When I think of a generic module, it is something that can be used
exactly as it is. The generic H-file and/or C-file, should not have to
be edited AT ALL in order to use it. If it has to be changed in just 1
line, it could possibly still have great reusability properties, but it
is not generic in the sense I mean.

I am going to give a suggestion for a way to do generics, by presenting
5 files as an example. I would very much like comments on how things
could be simplified or extended, but still using only C.

Ok, here we go. Lets say, we want to implement generic powers, by
repeating multiplication. The user of the module have to provide the
type, and a function that performs multiplication on that type.

In my opinion, that defeats the purpose of generic programming.

<snip>


santosh wrote:

ttl_id...@yahoo.com wrote:

This post is about how to get generic modules in a C environment. The
aim is to get close to Ada generics. Using C++ is not an option. It
should all run in a simple C compiler.

When I think of a generic module, it is something that can be used
exactly as it is. The generic H-file and/or C-file, should not have to
be edited AT ALL in order to use it. If it has to be changed in just 1
line, it could possibly still have great reusability properties, but it
is not generic in the sense I mean.

I am going to give a suggestion for a way to do generics, by presenting
5 files as an example. I would very much like comments on how things
could be simplified or extended, but still using only C.

Ok, here we go. Lets say, we want to implement generic powers, by
repeating multiplication. The user of the module have to provide the
type, and a function that performs multiplication on that type.


In my opinion, that defeats the purpose of generic programming.

The purpose of generics is to write algorithms once, and allow them to
apply to different types. The OP''s proposal is not as general as it
could be, but it is *one* form of generic programming. I give another
more general approach in the following examples:

http://www.pobox.com/~qed/ll.zip
http://www.pobox.com/~qed/gstack.zip

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/


"santosh" <sa*********@gmail.comwrites:

ttl_id...@yahoo.com wrote:

[...]

>Ok, here we go. Lets say, we want to implement generic powers, by
repeating multiplication. The user of the module have to provide the
type, and a function that performs multiplication on that type.


In my opinion, that defeats the purpose of generic programming.

<snip>

I disagree. It would be nice if the generic module could infer the
appropriate multiplication operator given the type (<OT>as Ada
generics can do</OT>), but that''s difficult or impossible to do in C.

Note also that it doesn''t just *require* you to provide a
multiplication function, it *allows* you to do so. If you provide a
function that performs addition, the generic will perform
multiplication. It provides a generic way to specify applying some
arbitrary operation repeatedly. In a sense, that''s more generic than
just providing a power function.

(I haven''t read the code closely enough to verify that this will
actually work.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于C中的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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