Does C support overloading?
I just want to know if C supports over loading? As we use 开发者_如何学Pythonsystem functions like printf with different no of arguments. Help me out
No, C doesn't support any form of overloading (unless you count the fact that the built-in operators are overloaded already, to be a form of overloading).
printf
works using a feature called varargs. You make a call that looks like it might be overloaded:
printf("%d", 12); // int overload?
printf("%s", "hi"); // char* overload?
Actually it isn't. There is only one printf function, but the compiler uses a special calling convention to call it, where whatever arguments you provide are put in sequence on the stack[*]. printf (or vprintf) examines the format string and uses that to work out how to read those arguments back. This is why printf isn't type-safe:
char *format = "%d";
printf(format, "hi"); // undefined behaviour, no diagnostic required.
[*] the standard doesn't actually say they're passed on the stack, or mention a stack at all, but that's the natural implementation.
C does not support overloading. (Obviously, even if it did, they wouldn't use that for printf: you'd need a printf for every possible combination of types!)
printf uses varargs.
No, C does not support overloading, but it does support Variadic functions. printf is an example of Variadic functions.
It all depends on how you define "support".
Obviously, C language provides overloaded operators within the core language, since most operators in C have overloaded functionality: you can use binary +
with int
, long
and with pointer types.
Yet at the same time C does not allow you to create your own overloaded functions, and C standard library also has to resort to differently-named functions to be used with different types (like abs
, fabs
, labs
and so on).
In other words, C has some degree of overloading hardcoded into the core language, but neither the standard library nor the users are allowed to do their own overloading.
No, C doesn't support overloading. If you want to implement overloading similar to C++, you will have to mangle your function names manually, using some sort of consistent convention. For example:
int myModule_myFunction_add();
int myModule_myFunction_add_int(int);
int myModule_myFunction_add_char_int(char, int);
int myModule_myFunction_add_pMyStruct_int(MyStruct*, int);
There is no provision in the C standard for operator overloading; proposals to add it have been rejected on the basis that many build systems have no facility to accommodate multiple functions with the same name. While C++ can work around this by e.g. having
void foo(int);
int foo(char*);
long foo(char *, char **);
compile to functions named something like v__foo_i, i__foo_pc, and l__foo_pc_ppc [compilers use different naming conventions, though the C++ standard forbids the use of internal double-underscores in identifiers so as to allow compilers to give things names like the above without conflict]. The authors of the C standard did not want to require any compilers to change naming conventions to allow for overloading, so they don't provide for it.
It would be possible and useful for a compiler to allow overloading of static and inline functions without creating naming problems; this would in practice be just as useful as allowing overloading of externally-linkable functions since one could have a header file:
void foo_zz1(int);
int foo_zz2(char*);
long foo_zz3(char *, char **);
inline void foo(int x) { foo_zz1(x); }
inline int foo(char* st) { foo_zz2(st); }
long foo(char *p1, char **p2) { foo_zz3(p1,p2); }
I recall looking at an embedded compiler for a hybrid between C and C++ which supported the above as a non-standard extension, but I'm not positive about the details. In any case, even if some C compilers do support overloading of functions which do not have external linkage, it is not supported by C14 nor am I aware (unfortunately) of any active efforts to add such a feature to future C standards.
Nonetheless, GCC can be made, using macros, to support a form of overloading which is not supported directly in languages with operator overloading. GCC includes an intrinsic which will identify whether an expression can be evaluated as a compile-time constant. Using this intrinsic, one can write a macro which can evaluate an expression different ways (including by calling functions) depending upon the argument. This can be useful in some cases where a formula would evaluate as a compile-time constant if given a compile-time constant argument, but would yield a horrible mess if given a variable argument. As a simple example, suppose one wishes to bit-reverse a 32-bit value. If the value is constant, one could do that via:
#define nyb_swap(x) \
((((x) & 1)<<3) | (((x) & 2)<<1) | (((x) & 4)>>1) | ((((x) & 8)>>3) )
#define byte_swap(x) \
( (nyb_swap(x)<<4) | nyb_swap((x) >> 4) )
#define word_swap(x) \
( (byte_swap(x)<<24) | (byte_swap((x) >> 8)<<16) | \
(byte_swap((x) >> 16)<<8) | (byte_swap((x) >> 24)) )
And an expression like uint32_t x=word_swap(0x12345678);
would simply load x
with 0x87654321. On the other hand, if the value is not a constant, the result would be horrible: an expression like uint32_t y=word_swap(x);
might generate many dozens of instructions; a call to a function with a partially-unrolled loop would be almost as fast but a lot more compact. On the other hand, using a loop would prevent the result from being regarded as a compile-time constant.
Using GCC, one can define a macro which will either use the constant-yielding macro if given a constant, or call a function when given a variable:
#define wswap(x) \
(__builtin_constant_p((x)) ? word_swap((x)) : word_swap_func((x))
This approach can't do everything type-based overloading can do, but it can do many things overloading can't.
Not directly, and this is not how printf
works, but it is possible to create the equivalent of overloaded functions using macros if the types are of different sizes. The type-generic math functions in tgmath.h of the C99 standard may be implemented in that manner.
C Does not support overloading. But we can implement that functionality by programming our own library that in turn could provide overloading support.
No c does not support function overloading. But you can get it to compile/work if you are using g++ (a c++ compiler).
精彩评论