function overloading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lordkain@tlen.pl

    #1

    function overloading

    is it possible to do some kind of function overloading in c? and that
    the return type is different

  • Richard Heathfield

    #2
    Re: function overloading

    lordkain@tlen.p l said:
    is it possible to do some kind of function overloading in c? and that
    the return type is different
    Function overloading is not *supported* in C. There is no syntax for it, so
    to speak. Whether it is *possible* is another question. I would answer an
    unequivocal "no", were it not for the fact that comp.lang.c subscribers
    frequently surprise me with their ingenuity! Nevertheless, any solution is
    likely to be so contorted that you may find it better to find another way
    around your problem.

    I may be mistaken here, but I have a hunch that you have not asked the
    question you perhaps ought to be asking. It seems to me that there is some
    task (call it X) that you need to achieve, and you've thought about it, and
    decided that function overloading would be a way to achieve that task, and
    so you're asking about function overloading, whereas you may find it more
    productive to ask about ways in which X itself, whatever it may be, can be
    achieved.

    If any practical programming language is Turing-complete, so is C. (And if
    none is, C fails in that regard only in the same way that other languages
    do.) Therefore, if it's computable, C can compute it. That does not,
    however, mean "if some other language has feature F, then C must have
    feature F too". Different languages provide different ways of getting from
    a problem to a solution. Just because C doesn't support function
    overloading, that doesn't mean you can't solve your /real/ problem in C.

    Incidentally, you hint that you wish to be able to distinguish between
    functions of the same name purely by virtue of their having different
    return types. Even C++ (which *does* support function overloading) cannot
    do this.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: normal service will be restored as soon as possible. Please do not
    adjust your email clients.

    Comment

    • Chris Dollin

      #3
      Re: function overloading

      Richard Heathfield wrote:
      Incidentally, you hint that you wish to be able to distinguish between
      functions of the same name purely by virtue of their having different
      return types. Even C++ (which *does* support function overloading) cannot
      do this.
      There are languages which do. I /think/ Ada is one of them. But this
      is a fuzzy memory from -- ye gods and little finches -- more than 20
      years ago.

      --
      Chris "hantwig efferko VOOM!" Dollin
      "We did not have time to find out everything we wanted to know."
      - James Blish, /A Clash of Cymbals/

      Comment

      • Jean-Marc Bourguet

        #4
        Re: function overloading

        Chris Dollin <chris.dollin@h p.comwrites:
        Richard Heathfield wrote:
        >
        Incidentally, you hint that you wish to be able to distinguish between
        functions of the same name purely by virtue of their having different
        return types. Even C++ (which *does* support function overloading) cannot
        do this.
        >
        There are languages which do. I /think/ Ada is one of them. But this
        is a fuzzy memory from -- ye gods and little finches -- more than 20
        years ago.
        Ada is able to do it.

        C++ is not able to do it but is able to somewhat fake it. The way is to
        return a proxy which and do all the hard work user conversion operators.

        A+

        --
        Jean-Marc

        Comment

        • Harald van Dijk

          #5
          Re: function overloading

          Richard Heathfield wrote:Incidenta lly, you hint that you wish to be
          able to distinguish between functions of the same name purely by
          virtue of their having different return types. Even C++ (which *does*
          support function overloading) cannot do this.<OT>Yes, it can, in
          limited cases. An example (of poor C++-style):#include
          <stdio.h>namesp ace A { void foo() { puts("void foo()"); }}namespace B
          { int foo() { puts("int foo()"); return 0; }}using A::foo;using
          B::foo;int main() { ((void(*)()) foo)(); ((int (*)()) foo)();}</OT>

          Comment

          • KISHORE

            #6
            Re: function overloading


            lordkain@tlen.p l wrote:
            is it possible to do some kind of function overloading in c? and that
            the return type is different

            Since If we observe The limitations of c,and list out them we can
            conclude that function overloading cannot be done in c language.thats
            why there r seperate functions for different types in <math.hto
            clarify ur doubt open turboc editor and open math.h header file and
            gothrough the function declerations.u can know the answer.

            Comment

            • Nils O. Selåsdal

              #7
              Re: function overloading

              KISHORE wrote:
              lordkain@tlen.p l wrote:
              >is it possible to do some kind of function overloading in c? and that
              >the return type is different
              >
              >
              Since If we observe The limitations of c,and list out them we can
              conclude that function overloading cannot be done in c language.thats
              why there r seperate functions for different types in <math.hto
              clarify ur doubt open turboc editor and open math.h header file and
              gothrough the function declerations.u can know the answer.
              Unless the files that comes with TurboC are crafted to work with
              TurboC only, and are not really standard C.
              (substitute TurboC with whatever random implementation on your hand)

              Comment

              • Richard Heathfield

                #8
                Re: function overloading

                [Broken flow fixed]

                Harald van D?k said:
                Richard Heathfield wrote:
                >Incidentally , you hint that you wish to be able to distinguish between
                >functions of the same name purely by virtue of their having different
                >return types. Even C++ (which *does* support function overloading) cannot
                >do this.
                >
                <OT>Yes, it can,
                No, it can't.
                in limited cases. An example (of poor C++-style):
                >
                #include <stdio.h>
                namespace A { void foo(){puts("voi d foo()");}}
                namespace B { int foo(){puts("int foo()"); return 0;}}
                using A::foo;using B::foo;
                int main() {((void(*)()) foo)(); ((int (*)()) foo)();}
                </OT>
                Your example does not distinguish between functions of the same name purely
                by virtue of their having different return types. It also requires them to
                be in different namespaces, and thus the return type is not the only
                distinguishing characteristic.

                --
                Richard Heathfield
                "Usenet is a strange place" - dmr 29/7/1999

                email: normal service will be restored as soon as possible. Please do not
                adjust your email clients.

                Comment

                • CBFalconer

                  #9
                  Re: function overloading

                  Harald van D?k wrote:
                  >
                  Richard Heathfield wrote:Incidenta lly, you hint that you wish to be
                  able to distinguish between functions of the same name purely by
                  virtue of their having different return types. Even C++ (which *does*
                  support function overloading) cannot do this.<OT>Yes, it can, in
                  limited cases. An example (of poor C++-style):#include
                  <stdio.h>namesp ace A { void foo() { puts("void foo()"); }}namespace B
                  { int foo() { puts("int foo()"); return 0; }}using A::foo;using
                  B::foo;int main() { ((void(*)()) foo)(); ((int (*)()) foo)();}</OT>
                  A shining example of why not to use the faulty google interface to
                  usenet.

                  --
                  Chuck F (cbfalconer at maineline dot net)
                  Available for consulting/temporary embedded and systems.
                  <http://cbfalconer.home .att.net>

                  Comment

                  • Stephen Sprunk

                    #10
                    Re: function overloading

                    <lordkain@tlen. plwrote in message
                    news:1163768205 .539772.176600@ k70g2000cwa.goo glegroups.com.. .
                    is it possible to do some kind of function overloading in c? and that
                    the return type is different
                    In general, no; C does not support overloading as part of the language.
                    However, if you have a C99 system, take a look at how <tgmath.his
                    implemented. Depending on what you're doing, something similar may
                    work, but you won't end up with portable code. In general, it's better
                    to find a solution that doesn't require overloading in the first place.

                    My implementation does something similar to this:

                    int foo_char(char x);
                    int foo_int(int x);
                    #define foo(x) { if (sizeof(x)==siz eof(char)) foo_char(x); else
                    foo_int(x); }

                    This, of course, assumes that all of the possible arguments to your
                    "overloaded " function can be distinguished by size; that's not a
                    portable assumption. Too bad there's no standard "typeof" operator.

                    S

                    --
                    Stephen Sprunk "God does not play dice." --Albert Einstein
                    CCIE #3723 "God is an inveterate gambler, and He throws the
                    K5SSS dice at every possible opportunity." --Stephen Hawking



                    --
                    Posted via a free Usenet account from http://www.teranews.com

                    Comment

                    • Keith Thompson

                      #11
                      Re: function overloading

                      CBFalconer <cbfalconer@yah oo.comwrites:
                      Harald van D?k wrote:
                      >Richard Heathfield wrote:Incidenta lly, you hint that you wish to be
                      >able to distinguish between functions of the same name purely by
                      >virtue of their having different return types. Even C++ (which *does*
                      >support function overloading) cannot do this.<OT>Yes, it can, in
                      >limited cases. An example (of poor C++-style):#include
                      ><stdio.h>names pace A { void foo() { puts("void foo()"); }}namespace B
                      >{ int foo() { puts("int foo()"); return 0; }}using A::foo;using
                      >B::foo;int main() { ((void(*)()) foo)(); ((int (*)()) foo)();}</OT>
                      >
                      A shining example of why not to use the faulty google interface to
                      usenet.
                      Since I've never seen this particular problem in a Google post, it's
                      not clear that Google is at fault.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • Tor Rustad

                        #12
                        Re: function overloading

                        Stephen Sprunk skrev:

                        [...]
                        My implementation does something similar to this:
                        >
                        int foo_char(char x);
                        int foo_int(int x);
                        #define foo(x) { if (sizeof(x)==siz eof(char)) foo_char(x); else
                        foo_int(x); }
                        You should consider using the macro

                        #define foo(x) ((sizeof((x))== sizeof(char) ? (foo_char((x))) :
                        (foo_int)((x)))

                        instead. However, this will not work in cases like

                        foo( foo(x) )

                        so I guess most C99 implementations use compiler magic

                        #define foo(x) __foo_generic(( x), foo_char, foo_int)

                        --
                        Tor <torust AT online DOT no>

                        Comment

                        • Harald van Dijk

                          #13
                          Re: function overloading

                          Keith Thompson wrote:
                          CBFalconer <cbfalconer@yah oo.comwrites:
                          Harald van D?k wrote:
                          Richard Heathfield wrote:Incidenta lly, you hint that you wish to be
                          able to distinguish between functions of the same name purely by
                          virtue of their having different return types. Even C++ (which *does*
                          support function overloading) cannot do this.<OT>Yes, it can, in
                          limited cases. An example (of poor C++-style):#include
                          <stdio.h>namesp ace A { void foo() { puts("void foo()"); }}namespace B
                          { int foo() { puts("int foo()"); return 0; }}using A::foo;using
                          B::foo;int main() { ((void(*)()) foo)(); ((int (*)()) foo)();}</OT>
                          A shining example of why not to use the faulty google interface to
                          usenet.
                          >
                          Since I've never seen this particular problem in a Google post, it's
                          not clear that Google is at fault.
                          I would've commented sooner, but I didn't want to mess up again. I'm
                          not sure what happened myself. In Google's preview, my message appeared
                          okay, but after posting, well, you saw what happened. The only thing I
                          did differently was using w3m instead of konqueror. I'll avoid using
                          that to post in the future, so hopefully it'll not happen again.

                          Comment

                          • lordkain@tlen.pl

                            #14
                            Re: function overloading

                            Hi Richard,
                            Richard Heathfield schreef:
                            I may be mistaken here, but I have a hunch that you have not asked the
                            question you perhaps ought to be asking. It seems to me that there is some
                            task (call it X) that you need to achieve, and you've thought about it, and
                            decided that function overloading would be a way to achieve that task, and
                            so you're asking about function overloading, whereas you may find it more
                            productive to ask about ways in which X itself, whatever it may be, can be
                            achieved.
                            You're absolutly right. Im trying to do a task called X and figured out
                            that the best option could be function overloading. My real task and
                            burning question that I'm bound to solve is to be found at


                            or look at the subject [xml to function call] in the google newsgroup.

                            Cheerz, David

                            Comment

                            • Richard Heathfield

                              #15
                              Re: function overloading

                              lordkain@tlen.p l said:
                              or look at the subject [xml to function call] in the google newsgroup.
                              It looks to me like you just need to write an XML-to-C translator for
                              building your test harness. I don't see any need for function overloading.
                              Eric S seems to have given you some good tips.

                              --
                              Richard Heathfield
                              "Usenet is a strange place" - dmr 29/7/1999

                              email: rjh at the above domain, - www.

                              Comment

                              Working...