ANSI C and Function Overloading

Possible duplicate:
function overload in C

ANSI C does not allow function overloading (I'm not sure about C99).

eg:

char max(char x, char y); short max(short x, short y); int max(int x, int y); float max(float x, float y); 

is not valid ANSI C source code.

What technique (or idea) should be used for the problem of function overloading in ANSI C?

Note

The answer renames the functions, but which template should be used to rename, these function names remain the "good function name" ?

eg:

 char max1(char x, char y); short max2(short x, short y); int max3(int x, int y); float max4(float x, float y); 

is not a good name for max function name.

+6
source share
2 answers

Using a data type for evaluation in a function name, for example

 char max_char(char x, char y); short max_short(short x, short y); int max_int(int x, int y); float max_float(float x, float y); 
+11
source

In this example, the correct solution uses a macro. You can also just use the built-in function, which uses the largest possible integer or floating-point type and allows the compiler to optimize it when it is known that the argument is less. There are some angular cases that you should consider regarding subscription, etc., but this happens anyway.

0
source

Source: https://habr.com/ru/post/899683/


All Articles