Can a set of functions differ from a name depending on the types of operands that are theoretically statically polymorphic in C?

Is a set of functions that perform exactly one word, but different from a name like atoi , atol , atoll , etc., called theoretically polymorphic?

For example, I have a say swap function that should work with various data types. Therefore, I have one function / operation for which I have different implementations, but, unfortunately, because the language does not support the use of the same function name, to get such functionality, I need to make options swap_i , swap_l , swap_f , swap_str etc etc. and call them manually depending on the operands.

When developing code, one could design this as one function with various implementations that are called depending on the operands. But in this case, instead of the compiler, the programmer needs to make a static binding of functions. If a programmer implemented it in C ++, then the design would be the same (as he / she followed the OOD approach), but in this case the static binding can be done by the compiler without disturbing the user.

Now, if there was a polymorphic function in the swap design approach, would such a difference in the implementation of the C and C ++ design matter, so we cannot call such an implementation of C not polymorphic, but the C ++ implementation polymorphic?

EDIT1:

Another example:

let's say we send a union with the various possible data types packed into it, and a structure wrapping it with a variable indicating which component of the union will be used. Then we can use only one swap function name with no options. After we get the structure in the function, we can internally perform operations depending on the type of data active in the union

  struct _generic { int type; union { int a; float b; char c; } component; } variable; swap (struct _generic var2, struct _generic var1) { if (var1.type == INTEGER) { /* code for integer */ } else if (var1.type == FLOAT) { /* code for float */ } . . . } 

In this case, we use a different method.

Also pay attention to the ellipse operator ... , as in printf , with this you can also get some built-in function.

The main thing is that the C language does not provide a direct implementation of OOD s, but can we use the OOD terminology for implementation in a C implementation if it is developed using object-oriented design?

+4
source share
5 answers

In my opinion, this is not true, because polymorphism and other object-oriented terms are not what depends on the language, and this is a concept. Now different languages ​​provide different functions with which the implementation of the object model is much simpler, and some functions are possible only in other languages.

If one problem was modeled using the OOD approach, and the implementation with C and C ++ is used for the same model, supporting the same approach, where the difference comes only from a limited set of language functions, and then they are compiled, then both generate assembler code. C ++ outputs for a polymorphic function are no longer polymorphic after conversion to asm.

The difference is that C does not provide the ability to implement OOD as simple as it can be done in C ++ or other OOP languages.

0
source

Suppose the difference was two characters, or three, or ten? Can various functions be polymorphic? I would say no, they would not be. So what's so special about 1 character?

+3
source

This is not polymorphic because variants of swap functions cannot have more than one signature. The whole purpose of polymorphism is to limit the content performed by the programmer and think of it as simply overloading, but human-made is certainly the wrong way to think about it. The trick is the term poly and morphism. There is nothing multiple about any of these functions - they simply have similar functionality under similar names, and there is no morphing between them, so they are undoubtedly not polymorphic.

+2
source

I think you are mixing different concepts and languages, and this is a recipe for confusion. What you call polymorphic is usually called overloaded in C ++: there are different functions that share a name. Polymorphism usually refers to the property of overriding the behavior of a function in a base class. The function itself will have a fixed set of names, arguments, and return type (depending on the language, there is some space for covariant return types or contravariant arguments).

As for whether it is advisable to call a manual implementation in C as an overload, I don't think so. If you extend and insult this term, you can make it suitable, just like you can program an object-oriented assembler (some assemblers allow you to define structures, you can add addresses to other code characters as attributes for structures. Does is this an object oriented assembler?)

+1
source

C does not show polymorphism. http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 for information on what polymorphism is. In order for C functions to be considered even remotely polymorphic, all functions must have the same name, but work with different data types (for example, function overloading).

0
source

All Articles