overloaded functions in C

Discussion in 'C Programming' started by bahadir.balban@gmail.com, Jun 9, 2005.

  1. Guest

    Hi,

    What's the best way to implement an overloaded function in C? For
    instance if you want to have generic print function for various
    structures, my implementation would be with a case statement:

    void print_structs(void * struct_argument, enum struct_type stype)
    {
    switch(stype) {
    case STRUCT_TYPE_1:
    struct type1 * p = (struct type1 *) struct_argument;
    printf("type1.field1: %d\n", type1.field1);

    case STRUCT_TYPE_2:
    ...
    }
    return;
    }

    One thing I don't like about this is that, you must encode type
    information
    in an enum. I don't like this because I don't prefer encoding
    user-defined aspects into code in general. Is there a better way of
    doing this?

    Thanks,
    Bahadir
    , Jun 9, 2005
    #1
    1. Advertising

  2. jacob navia Guest

    wrote:
    > Hi,
    >
    > What's the best way to implement an overloaded function in C? For
    > instance if you want to have generic print function for various
    > structures, my implementation would be with a case statement:
    >
    > void print_structs(void * struct_argument, enum struct_type stype)
    > {
    > switch(stype) {
    > case STRUCT_TYPE_1:
    > struct type1 * p = (struct type1 *) struct_argument;
    > printf("type1.field1: %d\n", type1.field1);
    >
    > case STRUCT_TYPE_2:
    > ...
    > }
    > return;
    > }
    >
    > One thing I don't like about this is that, you must encode type
    > information
    > in an enum. I don't like this because I don't prefer encoding
    > user-defined aspects into code in general. Is there a better way of
    > doing this?
    >
    > Thanks,
    > Bahadir
    >

    lcc-win32 is a C compiler that implements overloaded functions.
    http://www.cs.virginia.edu/~lcc-win32

    Please:

    flames >/dev/null
    jacob navia, Jun 9, 2005
    #2
    1. Advertising

  3. Richard Bos Guest

    jacob navia <> wrote:

    > lcc-win32 is


    Phenomenally off-topic here, and you _know_ this, damn you, navia!

    > Please:
    >
    > flames >/dev/null


    Please: off-topic advertisements to your own playground newsgroup.

    Richard
    Richard Bos, Jun 9, 2005
    #3
  4. Eric Sosman Guest

    jacob navia wrote:
    >
    > lcc-win32 is a C compiler that implements overloaded functions.


    If it implements overloaded functions, it is not a C
    compiler.

    > Please:
    >
    > flames >/dev/null


    I have not objected to your self-promotion before; every
    useful signal carries some noise. But this latest post is the
    worst I have seen: it is a deliberate attempt to mislead. It is
    not merely advertising, but false advertising.

    I will send my future flames to /dev/null if you will send
    your future nonsense there, too. Deal?

    --
    Eric Sosman
    lid
    Eric Sosman, Jun 9, 2005
    #4
  5. jacob navia Guest

    Eric Sosman wrote:
    > jacob navia wrote:
    > >

    >
    >> lcc-win32 is a C compiler that implements overloaded functions.

    >
    >
    > If it implements overloaded functions, it is not a C
    > compiler.



    The ANSI C standard page 7:
    >>

    A conforming implementation may have extensions (including additional
    library functions), provided they do not alter the behavior of any
    strictly conforming program.3)
    >>


    And in the footnote 3 we have
    >>

    3) This implies that a conforming implementation reserves no identifiers
    other than those explicitly reserved in this International Standard.
    >>


    I have respected in the implementation of overloaded functions both
    points.

    So, even if you do not like it, lcc-win32 is a C compiler.
    jacob navia, Jun 9, 2005
    #5
  6. jacob navia Guest

    Richard Bos wrote:
    >
    > Phenomenally off-topic here, and you _know_ this, damn you, navia!
    >


    Since this newsgroup is too old to have a chart, this is your
    personal appreciation. I beg to differ.

    jacob
    jacob navia, Jun 9, 2005
    #6
  7. In article <42a83d43$0$1250$>,
    jacob navia <> wrote:
    >Richard Bos wrote:
    >>
    >> Phenomenally off-topic here, and you _know_ this, damn you, navia!
    >>

    >
    >Since this newsgroup is too old to have a chart, this is your
    >personal appreciation. I beg to differ.
    >
    >jacob


    Indeed. Well put.

    I think that your (Jacob's) standard response to this kind of BS should be
    to state:

    "Asserting something 1,000 times does not, in and of itself, make it true."
    Kenny McCormack, Jun 9, 2005
    #7
  8. On Thu, 09 Jun 2005 13:24:08 +0200, jacob navia
    <> wrote:

    > wrote:
    >> Hi,
    >>
    >> What's the best way to implement an overloaded function in C? For
    >> instance if you want to have generic print function for various
    >> structures, my implementation would be with a case statement:
    >>
    >> void print_structs(void * struct_argument, enum struct_type stype)
    >> {
    >> switch(stype) {
    >> case STRUCT_TYPE_1:
    >> struct type1 * p = (struct type1 *) struct_argument;
    >> printf("type1.field1: %d\n", type1.field1);
    >>
    >> case STRUCT_TYPE_2:
    >> ...
    >> }
    >> return;
    >> }
    >>
    >> One thing I don't like about this is that, you must encode type
    >> information
    >> in an enum. I don't like this because I don't prefer encoding
    >> user-defined aspects into code in general. Is there a better way of
    >> doing this?
    >>
    >> Thanks,
    >> Bahadir
    >>

    > lcc-win32 is a C compiler that implements overloaded functions.


    But not in C. In some vaguely C-like language, possibly.

    > http://www.cs.virginia.edu/~lcc-win32
    >
    > Please:
    >
    > flames >/dev/null


    Stop your false advertising, then.

    Chris C
    Chris Croughton, Jun 9, 2005
    #8
  9. On Thu, 09 Jun 2005 14:58:31 +0200, jacob navia wrote:

    ....

    > The ANSI C standard page 7:
    > >>

    > A conforming implementation may have extensions (including additional
    > library functions), provided they do not alter the behavior of any
    > strictly conforming program.3)
    > >>

    >
    > And in the footnote 3 we have
    > >>

    > 3) This implies that a conforming implementation reserves no identifiers
    > other than those explicitly reserved in this International Standard.
    > >>

    >
    > I have respected in the implementation of overloaded functions both
    > points.


    However these aren't the only requirements of a conforming implementation.
    It must for example generate a diagnostic if the program contains a syntax
    error or constraint violation. For example consider the constraint:

    6.7p4 All declarations in the same scope that refer to the same object or
    function shall specify compatible types.

    Of course you can meet this requirement by generating diagnostics and
    still compile the code, but it is usually easier (i.e. not to have to
    explain diagnostics to your users) to support extensions like this in a
    non-conforming mode.

    > So, even if you do not like it, lcc-win32 is a C compiler.


    Most compilers can be invoked in various modes, some of which might be
    conforming (or attempt to be) others not. So just saying that something is
    a C compiler barely begins to tell the story.

    Lawrence
    Lawrence Kirby, Jun 9, 2005
    #9
  10. jacob navia Guest

    Lawrence Kirby wrote:
    > Most compilers can be invoked in various modes, some of which might be
    > conforming (or attempt to be) others not. So just saying that something is
    > a C compiler barely begins to tell the story.


    If you invoke lcc-win32 with the flag

    -ansic

    it will not accept any overloaded functions.
    All other extensions aren't recognized either (operator
    overloading and default function arguments).
    jacob navia, Jun 9, 2005
    #10
  11. On Thu, 09 Jun 2005 17:59:06 +0200, jacob navia
    <> wrote:

    > Lawrence Kirby wrote:
    >> Most compilers can be invoked in various modes, some of which might be
    >> conforming (or attempt to be) others not. So just saying that something is
    >> a C compiler barely begins to tell the story.

    >
    > If you invoke lcc-win32 with the flag
    >
    > -ansic
    >
    > it will not accept any overloaded functions.
    > All other extensions aren't recognized either (operator
    > overloading and default function arguments).


    But in that mode it won't do what you claimed for it:

    > lcc-win32 is a C compiler that implements overloaded functions.


    Either it is in a mode where it accepts overloaded functions, in which
    case it is not a C compiler in that mode, or it is in -ansic mode where
    it doesn't accept overloaded functions (in that mode it may or may not
    be a C compiler for other reasons).

    So saying "lcc-win32 is a C compiler that implements overloaded
    functions" is false. It is a compiler which will compiler C /or/ this
    other language with overloaded functions, but it will not do both with
    the same program.

    Since it is also pushing your own product, it is advertising.

    The combination is "false advertising", which may be a crime in your
    country (it is in others)...

    Chris C
    Chris Croughton, Jun 9, 2005
    #11
  12. jacob navia Guest

    Chris Croughton wrote:
    > Either it is in a mode where it accepts overloaded functions, in which
    > case it is not a C compiler in that mode,
    >
    > Chris C


    Wrong. Please read the standard and see in page 7:
    >>

    A conforming implementation may have extensions
    >>


    Extensions are not forbidden, if implemented according to the
    constraints that I followed.

    The -ansic flag disallows any extensions, and all
    functions not in the standard. No windows.h no
    network, etc etc.

    Extensions are a common feature of most C compilers
    and systems. The standard sets a minimum language,
    but all implementation implement extensions in one way or
    another.

    Is gcc in non pedantic mode not a C compiler any more?

    And Microsoft with their __declspec(...) is no longer C?

    Let's be realistic and discuss the issues here.

    A user asked about how to implement overloaded functions because
    he needs them, they are useful. Most languages have this feature
    in one way or another, from C# to C++ passing through all OO
    languages, etc.

    I understand C not as a freezed language that shuns any
    improvement but as a language that should be as easy to
    use as possible and that allows to construct modern software.

    Overloaded functions allow more flexibility in the implementation
    and less impact in the memory space of the user. Calling a
    function
    sqrt_for_long_doubles
    sqrt_for_double
    sqrt_for_float
    just makes things more difficult to remember.

    This is a plain truth, so much so, that the standard specifies
    the overloaded function sqrt for all of those types.

    That was a good idea, that I made available to the users of my
    compiler system. Why the implementor should have something
    that user's can't use? If the implementor uses them, they are
    needed and the user have a right to use that too.

    Calling all those three functions sqrt (as the standard header
    tgmath defines) makes programming in C easier.

    I just made that idea available to the users.

    Let's stop the polemic and discuss the issues at hand. All of this
    "I am holier than you" discussion is a waste of time.

    Have you anything against genericity? Should be eliminate tgmath
    from the standard then?

    jacob
    jacob navia, Jun 9, 2005
    #12
  13. jacob navia Guest

    Lawrence Kirby wrote:
    >
    > However these aren't the only requirements of a conforming implementation.
    > It must for example generate a diagnostic if the program contains a syntax
    > error or constraint violation. For example consider the constraint:
    >
    > 6.7p4 All declarations in the same scope that refer to the same object or
    > function shall specify compatible types.
    >


    You misunderstand overloaded functions.
    suppose:

    int overloaded fn(Mytype *arg);
    int overloaded fn(MyOtherType *arg);

    They are *not* the same object of course. They are TWO
    different functions, that's the point!

    The user has the facility of calling those two objects
    with the same name with different argument signatures.

    To stay within the standard, the overloaded function sqrt
    refers to 3 different functions:
    sqrtl(long double), sqrt(double) and sqrt(float).

    This overloading doesn't mean that there is ONE sqrt function
    but three of course.

    My implementation will not accept:

    int overloaded fn(Mytype *arg) {... }
    int overloaded fn(Mytype *arg) {... }

    because THAT would be a redefinition of the same object, as the
    standard specifies!
    jacob navia, Jun 9, 2005
    #13
  14. jacob navia Guest

    jacob navia wrote:
    > sqrtl(long double), sqrt(double) and sqrt(float).


    Sorry that should have been
    > sqrtl(long double), sqrt(double) and sqrtf(float).


    forgot the 'f' in sqrt(float)
    jacob navia, Jun 9, 2005
    #14
  15. CBFalconer Guest

    jacob navia wrote:
    > wrote:
    >>
    >> What's the best way to implement an overloaded function in C? For
    >> instance if you want to have generic print function for various
    >> structures, my implementation would be with a case statement:
    >>

    .... snip ...
    >>

    > lcc-win32 is a C compiler that implements overloaded functions.
    > http://www.cs.virginia.edu/~lcc-win32


    Instead of deliberately trolling with this you could have simply
    said that this is a non-standard non-portable extension, available
    in lcc-win32 for those who wish to fool with it. Is that so hard?
    Then the clamor would probably be limited to pointing out the
    insecurities it raises.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jun 9, 2005
    #15
  16. Eric Sosman Guest

    jacob navia wrote:
    > [...]
    > A user asked about how to implement overloaded functions because
    > he needs them, they are useful. [...]


    The user asked, and I quote:

    > What's the best way to implement an overloaded function in C?


    Look carefully at the end of the question. Squint your eyes,
    and you might just be able to make out the requirement: "in C."
    He didn't ask how to implement overloaded functions in Java or
    Scheme or C++ or SmallTalk or in just any old language somebody
    decides to suggest, he asked how to implement them "in C."

    When you "answered" by suggesting he use a different language
    (to wit, your own private language), you were unresponsive. When
    you claimed that YOPL was in fact C, you were being untruthful.
    Now, you're just being unpleasant.

    --
    Eric Sosman, Jun 9, 2005
    #16
  17. On Thu, 09 Jun 2005 21:52:49 +0200, jacob navia
    <> wrote:

    > Chris Croughton wrote:
    >> Either it is in a mode where it accepts overloaded functions, in which
    >> case it is not a C compiler in that mode,
    >>
    >> Chris C

    >
    > Wrong. Please read the standard and see in page 7:
    > >>

    > A conforming implementation may have extensions
    > >>

    >
    > Extensions are not forbidden, if implemented according to the
    > constraints that I followed.


    No, you did not, you implemented them in the user namespace.

    > The -ansic flag disallows any extensions, and all
    > functions not in the standard. No windows.h no
    > network, etc etc.


    Not much use then, if it can't use external libraries and headers --
    which /are/ allowed by the standard.

    > Extensions are a common feature of most C compilers
    > and systems. The standard sets a minimum language,
    > but all implementation implement extensions in one way or
    > another.


    All? Well, perhaps you've checked every C compiler in existence.
    Although extensions are necessary for some features of C99.

    > Is gcc in non pedantic mode not a C compiler any more?


    Correct, it adds certain features (like asm) in the user namespace, thus
    implementing a language almost, but not quite, C (indeed, with no
    language specified the default is a language somewhere between C89 and
    C99, with extra non-standard bits).

    > And Microsoft with their __declspec(...) is no longer C?


    __declspec is in the implementation namespace, which is allowed by the
    standard. Of course, they have had other inconsistencies as well...

    > Let's be realistic and discuss the issues here.
    >
    > A user asked about how to implement overloaded functions because
    > he needs them, they are useful. Most languages have this feature
    > in one way or another, from C# to C++ passing through all OO
    > languages, etc.


    And the answer is "They don't exist in C". If you want them, use
    another language (C++ is a standard, for instance).

    > I understand C not as a freezed language that shuns any
    > improvement but as a language that should be as easy to
    > use as possible and that allows to construct modern software.


    It certainly does the latter. If you want "as easy to use as possible"
    I suspect you want LOGO or a form of BASIC.

    > Overloaded functions allow more flexibility in the implementation
    > and less impact in the memory space of the user. Calling a
    > function
    > sqrt_for_long_doubles
    > sqrt_for_double
    > sqrt_for_float
    > just makes things more difficult to remember.


    Well, if those were the names they'd be easy to remember but hard to
    type correctly. But fsqrt and lsqrt are pretty easy to remember,
    particularly since they are consistent with the other maths functions'
    naming. I would have liked to see dsqrt as a synonym for sqrt, just for
    consistency, but I'm not too fussed about it.

    > This is a plain truth, so much so, that the standard specifies
    > the overloaded function sqrt for all of those types.


    Only with the hideous tgmath.h extensions, which do indeed require
    extensions (but not actually overloading, although they have that
    effect).

    > That was a good idea, that I made available to the users of my
    > compiler system. Why the implementor should have something
    > that user's can't use? If the implementor uses them, they are
    > needed and the user have a right to use that too.


    Indeed, I think that if they were going to allow it at all then it
    should have been specified properly.

    BUT IT WASN'T. That is the point, that no matter how much you want it
    that feature IS NOT C.

    > Calling all those three functions sqrt (as the standard header
    > tgmath defines) makes programming in C easier.
    >
    > I just made that idea available to the users.


    And thereby created another language variant. It is not C, it is "C
    with extensions".

    > Let's stop the polemic and discuss the issues at hand. All of this
    > "I am holier than you" discussion is a waste of time.


    The issue is that a person asked whether overloaded functions are
    available in C. The correct answer is simple, they are not. You,
    however, attempted to mislead by pushing your almost-C compiler which
    has them as extensions (and without pointing out that this is a
    non-standard feature and is only available on one platform). That is
    fraud,

    > Have you anything against genericity? Should be eliminate tgmath
    > from the standard then?


    My preference is yes. It isn't needed (if you don't know what type you
    want, your design is faulty, if it matters then you need to be able to
    specify it). My next preference is that if they really think that
    overloading is useful then it should be made generally available as a
    standard feature.
    Chris Croughton, Jun 9, 2005
    #17
  18. S.Tobias Guest

    jacob navia <> wrote:
    > Eric Sosman wrote:
    > > jacob navia wrote:
    > > >

    > >
    > >> lcc-win32 is a C compiler that implements overloaded functions.

    > >
    > >
    > > If it implements overloaded functions, it is not a C
    > > compiler.



    > The ANSI C standard page 7:

    [snip quotes]

    > I have respected in the implementation of overloaded functions both
    > points.


    > So, even if you do not like it, lcc-win32 is a C compiler.


    1. Your compiler may be quite good and conforming (I don't know,
    I haven't tried it, and it's not for me to judge anyway),
    I think it is pretty clear that in this group particular
    extensions are not topical. Otherwise valid topics would include
    POSIX, Win32 API, __far pointers, __cdecl(export) declarations,
    statement expressions, structured exceptions, __foos and __bars,
    and many more in heaven and earth than are dreamt of in your
    implementation.

    2. You have not explained to the OP that C does not support
    user defined function overloading, and in your implementation
    it is an extension, and that it probably works _only_ in your
    implementation. I don't think that inviting people to try
    new language features in your product is bad by itself, but
    I think that failing to mention that those features are
    not portable - is.

    3. All C++ compilers also support overloaded functions. So what?

    4. OP asked a question how you *implement* overloaded functions (I
    understand he meant the concept of overloaded functions).
    In what way is your answer relevant?

    5. Have you actually read the OP's article? Could you explain how
    function overloading would solve his problem? Because from
    what *I* have understood from his description, his needs are closer
    to the concept of polymorphic types, rather than overloading.

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jun 9, 2005
    #18
  19. jacob navia Guest

    Eric Sosman wrote:
    >
    > jacob navia wrote:
    >
    >>[...]
    >>A user asked about how to implement overloaded functions because
    >>he needs them, they are useful. [...]

    >
    >
    > The user asked, and I quote:
    >
    >
    >>What's the best way to implement an overloaded function in C?

    >
    >
    > Look carefully at the end of the question. Squint your eyes,
    > and you might just be able to make out the requirement: "in C."
    > He didn't ask how to implement overloaded functions in Java or
    > Scheme or C++ or SmallTalk or in just any old language somebody
    > decides to suggest, he asked how to implement them "in C."
    >
    > When you "answered" by suggesting he use a different language
    > (to wit, your own private language), you were unresponsive. When
    > you claimed that YOPL was in fact C, you were being untruthful.
    > Now, you're just being unpleasant.
    >


    Typical of you Eric:

    Just polemic, no arguments, no nothing.
    "A different language" ???

    Questions for you:

    1) Is "sqrt" an overloaded function or not?
    2) Isn't that defined in the C standard document?
    3) What you thing about the definitions in tgmath.h?
    ("tg" stands for "Type Generic" math).
    3) Why shouldn't this feature be available to all?

    What do you have against this feature besides that
    is an extension?
    jacob navia, Jun 9, 2005
    #19
  20. jacob navia Guest

    S.Tobias wrote:
    > 5. Have you actually read the OP's article? Could you explain how
    > function overloading would solve his problem? Because from
    > what *I* have understood from his description, his needs are closer
    > to the concept of polymorphic types, rather than overloading.


    Using overloaded functions the switch disappears and you have:

    void *overloaded print_structs(Type1 *p)
    {
    printf("Struct Type 1":\n");
    ...
    }


    void *overloaded print_structs(Type2 *p)
    {
    printf("Struct Type 2":\n");
    ...
    }

    etc.

    "Polymorphic types"... can you explain?

    By the way, the C standard does have overloaded functions:

    sqrt(double)
    sqrt(long double)
    sqrt(float)

    are the same as

    sqrtl(long double)
    sqrt(double)
    sqrtf(float)

    the "function" sqrt is overloaded to 3 different functions.
    And this is 100% standard C.

    Why should this feature not be available to users?

    That's the whole point here.

    jacob
    jacob navia, Jun 9, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Generic Usenet Account

    Overriding overloaded functions?

    Generic Usenet Account, Aug 27, 2003, in forum: C++
    Replies:
    2
    Views:
    370
    Anchor
    Aug 27, 2003
  2. kazack

    Overloaded functions

    kazack, Nov 27, 2003, in forum: C++
    Replies:
    3
    Views:
    258
    Stephen M. Webb
    Nov 27, 2003
  3. Jef Driesen
    Replies:
    1
    Views:
    459
    Michael Mellor
    Jan 29, 2004
  4. DX
    Replies:
    3
    Views:
    393
  5. DX
    Replies:
    0
    Views:
    343
Loading...

Share This Page