C function overloading?

Discussion in 'C Programming' started by JaSeong Ju, May 31, 2004.

  1. JaSeong Ju

    JaSeong Ju Guest

    I would like to overload a C function. Is there any easy way to do this?
     
    JaSeong Ju, May 31, 2004
    #1
    1. Advertisements

  2. Just compile with a C++ compiler.
     
    E. Robert Tisdale, May 31, 2004
    #2
    1. Advertisements

  3. No.

    Martien
     
    Martien Verbruggen, May 31, 2004
    #3
  4. JaSeong Ju

    Ben Pfaff Guest

    No. C does not support function overloading.
     
    Ben Pfaff, May 31, 2004
    #4
  5. JaSeong Ju

    Mabden Guest

    You could, however, write a wrapper function that calls the normal C
    function, but adds new functionality. Many people do this in calling memory
    functions, for instance.
     
    Mabden, May 31, 2004
    #5
  6. JaSeong Ju

    Ben Pfaff Guest

    Do you know what function overloading is? It is not changing the
    behavior of some particular function, i.e. as a wrapper would,
    which seems to be your implication.
     
    Ben Pfaff, May 31, 2004
    #6
  7. JaSeong Ju

    jacob navia Guest

    lcc-win32 is a compiler that supports function overloading.

    http://www.cs.virginia.edu/~lcc-win32

    It is not a C++ compiler
     
    jacob navia, May 31, 2004
    #7
  8. JaSeong Ju

    Ben Pfaff Guest

    If it supports function overloading then it isn't a C compiler
    either.
     
    Ben Pfaff, May 31, 2004
    #8
  9. JaSeong Ju

    Mabden Guest

    What a nasty question. Of course I do. Do YOU know what a C forum is for?
    Idiot.
     
    Mabden, May 31, 2004
    #9
  10. Standard C does not allow true function overloading, such as in C++.

    However, you may be able to simulate the effect with macros; examine
    <tgmath.h> for ideas how. Note that the method used by <tgmath.h> is
    necessarily implementation-dependent, so don't expect your code to be
    portable.

    S
     
    Stephen Sprunk, May 31, 2004
    #10
  11. Then please explain how a wrapper function and/or adding new
    functionality helps to solve the OP's problem.
    An ad hominem attack on a regular is reliable way to get into
    everybody's killfile. Soon, you might feel very lonely here.

    Martin
     
    Martin Dickopp, May 31, 2004
    #11
  12. There is no way in C at all.

    But what do you actually want to achieve? If you would post "I would
    like to overload a C function because..." then maybe you can get help to
    achieve what you want to achieve, without overloading a C function.
     
    Christian Bau, May 31, 2004
    #12
  13. Why?! [Yes, I know Jacob's lcc was raised, but speaking more generally...]

    Function overloading would require additional constraints on conforming programs using
    that extension (e.g. C++ requires function prototypes), but I don't see how support for
    function overloading would necessarily preclude implementation conformance.
     
    Peter Nilsson, May 31, 2004
    #13
  14. Only the old fashioned way...

    void my_function_int(int);
    void my_function_double(double);

    ....or by using judicious macros.

    Neither is as passe as some might have you think.

    As others have said, before you go rushing off to C++, perhaps you might like to explain
    if you're trying to do something specific. [And if so, what?]
     
    Peter Nilsson, May 31, 2004
    #14
  15. He he!

    http://mapage.noos.fr/emdel/clib.htm module SYSALLOC
     
    Emmanuel Delahaye, May 31, 2004
    #15
  16. JaSeong Ju

    jacob navia Guest

    I have taken care (and we discussed here in this group about it) to keep
    lcc-win32
    100% compatible with the current ANSI standard.

    The syntax proposed is:

    int overloaded fn(Type1 arg1);

    int overloaded fn(Type2 arg1);

    When the compiler sees the construction:
    fn(Type1) it will call the first one,
    fn(Type2) it will call the second one.
    else
    "Undefined call to overloaded function line 45"

    If the compiler sees:
    int fn(Type1 arg1);

    and LATER
    int overloaded fn(Type23);

    it will emit an error "function redefinition"

    In all cases "overloaded" is NOT a keyword:

    int overladed = 1;

    will *still work* as expected.
     
    jacob navia, May 31, 2004
    #16
  17. JaSeong Ju

    CBFalconer Guest

    It would not appear so.
    It is not a place for attacks on those more knowledgeable than
    you, who happen to point out your ignorance. It will take little
    of this to get yourself ignored.
     
    CBFalconer, May 31, 2004
    #17
  18. JaSeong Ju

    Joe Wright Guest

    Alright, impress me. What is meant by function overloading? Even
    though C does not support it, please show how to write a wrapper to
    do so.

    Is 'idiot' a term of endearment to you? If not, it is probably more
    applicable to you than it is to Ben Pfaff.
     
    Joe Wright, May 31, 2004
    #18
  19. Not at the C level I think, but you can do it at the linker level. With
    clever ordering of linking library you can replace pretty much any standard
    libray call. Of course this is a hack, not portable and depends on linker
    behavior. Furthermore; If you try to replace memcpy for example, it may not
    work cause some compiler use inline code for memcpy and alike. Using this
    solution is risky.
     
    Mario Charest, May 31, 2004
    #19
  20. If I understand it correctly, the compiler would have to tell you the
    first time it encounters something that is not pure Standard C, and then
    it can go on doing whatever it likes. So if you write code by mistake
    that is not Standard C but would be legal if function overloading were
    part of C, then the compiler MUST tell you. But it can allow function
    overloading after that.
     
    Christian Bau, May 31, 2004
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.