Operator overloading in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jacob navia

    Operator overloading in C

    C++ introduced an interesting feature (among others): operator overloading.

    The idea is to build a mechanism for the user defining its own number types and the operations to be
    done with them.

    I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350
    bit floats.

    Yes, 350. We have a lot of fast machines now. Why not use them? The extra-precision is useful in
    some algorithms.

    This extension to C is transparent, since it doesn't change anything in the basics of the language.
    The syntax used is:

    TYPE operator+(TYPE a,TYPE b)
    {
    }

    When in your program you use
    TYPE a,b;
    ...
    a+b;
    Means that you call the operator function with those two numbers. This is similar to C++, but since
    there are no classes (C is not object oriented) everything is quite simple.

    Note that this is an extension compatible with C and it is important to know that the C standard
    doesn't forbid extensions. They should not introduce new keywords though.

    In my implementation it means that

    int operator = 6;

    still works as it should. No new keywords.

    The operator symbol is recognized if (and only if):
    1) The level is global, i.e. not within a function definition.
    2) It is followed by one of the defined operator symbols (+, -, etc)
    3) This must be followed by a normal function definition. The name of this function is the sequence
    of types in the argument list, the "signature" in technical terms.

    Is this useful?

    Yes, for numbers it is ideal. It is less so, when the data types aren't quantities but other
    relationships or data. Take, for instance
    String a,b,c;
    ...
    c = a+b;

    This means that a+b is different from b+a. Not very clear.

    Operator overloding *can* be useful, specially for letting the users build new kinds of numbers,
    that fulfill special needs.

    What do you think?

    Voice your opinion. In any case Lcc-win32 is available at:




  • bd

    #2
    Re: Operator overloading in C

    On Fri, 08 Aug 2003 21:53:32 +0200, jacob navia wrote:
    [color=blue]
    > C++ introduced an interesting feature (among others): operator overloading.
    >
    > The idea is to build a mechanism for the user defining its own number types and the operations to be
    > done with them.
    >
    > I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350
    > bit floats.[/color]

    [snip]

    This is offtopic for comp.lang.c. comp.std.c is for discussion of changes
    to the standard.

    --
    Freenet distribution not available
    "Let's show this prehistoric bitch how we do things downtown!"
    -- The Ghostbusters

    Comment

    • Martijn

      #3
      Re: Operator overloading in C

      bd wrote:[color=blue][color=green]
      >> I decided to build it in the lcc-win32 distribution, and this
      >> feature allowed me to introduce 350 bit floats.[/color]
      >
      > [snip]
      >
      > This is offtopic for comp.lang.c. comp.std.c is for discussion of
      > changes
      > to the standard.[/color]

      How do you gather that he is trying to change the standard? He is merely
      suggesting an extension. I quote: "and it is important to know that the C
      standard doesn't forbid extensions".

      I guess is more of a moral one (which may or may not be off topic).

      In regards to the original question: I wouldn't use it because I prefer to
      stick to the standard, but I find the concept very neat and I do think it
      would be a useful extension which could clarify and simplify code.

      Good luck,

      --
      Martijn Haak



      Comment

      • jacob navia

        #4
        Re: Operator overloading in C


        "David Rubin" <bogus_address@ nomail.com> wrote in message news:3F340B97.8 E4F97FF@nomail. com...[color=blue]
        > jacob navia wrote:
        >
        > [snip - I introduced operator overloading as an extension to my compiler
        > suite][color=green]
        > > What do you think?[/color]
        >
        > If you want to use C++ features, use C++.
        >
        > /david[/color]

        Obviously I do not agree with that. C++ is a very complex language, and is missing an essential
        facility I found only in C: I like to build my structures/data as I want, without any object
        oriented framework imposed by the language.

        There are many things innovative in C++, but not all of it. This idea solves a problem that can't be
        solved in C easily, without adding any C++ complexity.

        jacob


        Comment

        • jacob navia

          #5
          Re: Operator overloading in C


          "bd" <bdonlan@bd-home-comp.no-ip.org> wrote in message
          news:pan.2003.0 8.08.20.43.05.7 93173@bd-home-comp.no-ip.org...[color=blue]
          > On Fri, 08 Aug 2003 21:53:32 +0200, jacob navia wrote:
          >[color=green]
          > > C++ introduced an interesting feature (among others): operator overloading.
          > >
          > > The idea is to build a mechanism for the user defining its own number types and the operations[/color][/color]
          to be[color=blue][color=green]
          > > done with them.
          > >
          > > I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce[/color][/color]
          350[color=blue][color=green]
          > > bit floats.[/color]
          >
          > [snip]
          >
          > This is offtopic for comp.lang.c. comp.std.c is for discussion of changes
          > to the standard.
          >[/color]

          I wouldn't say the standard should be changed, at least not before a good discussion. This is what
          comp.lang.c is all about isn't it?

          Discussion about C.

          Operator overloading allows the easy introduction of new numeric types without much problems. I am
          not saying is "the solution" for all problems but is "a solution" for the introduction of
          1) Higher precision numbers
          2) Bignums (arbitrary precision numbers)
          3) Rationals (Numbers expressed as integer ratios 5/9, for instance)
          4) Quaternions

          and many others. To do this in C requires
          divide(add(a,b) ,sub(a,b))
          instead of
          (a+b)/(a-b)

          jacob


          Comment

          • Malcolm

            #6
            Re: Operator overloading in C


            "jacob navia" <jacob.navia@ja cob.remcomp.fr> wrote in message[color=blue]
            >
            > Operator overloading allows the easy introduction of new numeric
            > types without much problems
            >[/color]
            I wrote a big number package in C++. It had plenty of problems, for instance
            I added a user defined cast to long to try to simplify the code, and
            promptly broke everything I had written previously.



            Comment

            • jacob navia

              #7
              Re: Operator overloading in C


              "Malcolm" <malcolm@55bank .freeserve.co.u k> wrote in message news:bh2kjr$2fd $1@news6.svr.po l.co.uk...[color=blue]
              >
              > "jacob navia" <jacob.navia@ja cob.remcomp.fr> wrote in message[color=green]
              > >
              > > Operator overloading allows the easy introduction of new numeric
              > > types without much problems
              > >[/color]
              > I wrote a big number package in C++. It had plenty of problems, for instance
              > I added a user defined cast to long to try to simplify the code, and
              > promptly broke everything I had written previously.[/color]

              Well, give lcc-win32 a try. I will be glad to help you if you find problems with it.
              jacob


              Comment

              • jacob navia

                #8
                Re: Operator overloading in C


                "Zeljko Vrba" <mo.dor@fly.srk .fer.hr> wrote in message news:slrnbj9lts .ntn.mordor@fly .srk.fer.hr...[color=blue]
                > In article <bh0vl6$nfk$1@n ews-reader5.wanadoo .fr>, jacob navia wrote:[color=green]
                > >
                > > What do you think?
                > >[/color]
                > I wouldn't use it because of:
                > - it is supported only by your compiler; if I want to make my program available
                > to general public (i.e. UNIX systems, non x86 systems), the feature is useless
                > - I don't find prefix, function-call synax hard, and it's standard C
                > - unclear semantics: given
                >
                > BIGNUM operator+(BIGNU M, double)
                >
                > and an expression like a+2 (where a is BIGNUM), will 2 get converted to
                > double or an error will result?
                >[/color]
                If you compile this
                #include <stdio.h>
                typedef struct tagB {
                int a;
                int b;
                } B;

                B operator+(B a,double d)
                {
                printf("operato r + called\n");
                return a;
                }

                int main(void)
                {
                B a,b;

                a = a+2;
                return 0;
                }
                This produces
                "operator + called"
                [color=blue]
                > Will this allow me to define additional
                > BIGNUM operator+(BIGNU M, int)
                >[/color]
                Yes. But in that case the int operator+ will be called. The algorithm first seeks a correct match
                without any promotions. Then it will do "the usual conversions", then it will look if there are
                defined casts that could match.[color=blue]
                > I say C++ features are best left to C++.[/color]

                I say operator overloading is useful.


                Comment

                • Stefano Ghirlanda

                  #9
                  Re: Operator overloading in C

                  "jacob navia" <jacob.navia@ja cob.remcomp.fr> writes:
                  [color=blue]
                  > C++ introduced an interesting feature (among others): operator
                  > overloading.[/color]

                  <explanation snipped>

                  Just my 2c: operator (and function) overloading are about the only C++
                  features I miss since I switched (back) to C.

                  I think a number of replies to your post reflect the fear that, as
                  soon as one starts to introduce new features, it will be impossible to
                  stop and C will soon become as complex as C++. And I don't deny that
                  there may be some truth in this claim.

                  Good luck,

                  --
                  Stefano

                  Comment

                  • Zeljko Vrba

                    #10
                    Re: Operator overloading in C

                    In article <bh2ohn$a1e$1@n ews-reader3.wanadoo .fr>, jacob navia wrote:[color=blue]
                    >
                    > I say operator overloading is useful.
                    >[/color]
                    I didn't say it isn't. But then use C++. Nobody forces you to use classes,
                    inheritance, access levels and other C++ novelties. You can use it just as
                    an "enhanced C" using only features that you like/need/feel comfortable with.

                    Don't get me wrong: you did a tremendous amount of work coding a C compiler
                    and I admire that (having listened through 2 semesters of formal languages
                    and automata theory at faculty I know what is involved :)).

                    I just don't feel comfortable about spreading around another variant of C
                    with its own extensions that are incompatible with others in a fundamental
                    way (code written for your compiler would need a major revision if/when need
                    arises to port it to ANSI C).

                    Diversity of dialects killed LISP (the only other language I find beautiful
                    besides C) and Basic (at least until Gates "standardiz ed" it). We shouldn't
                    be doing it to C too.

                    Comment

                    • jacob navia

                      #11
                      Re: Operator overloading in C


                      "Stefano Ghirlanda" <Stefano.Ghirla nda@intercult.s u.se> wrote in message
                      news:87vft73vjb .fsf@ethology.i ntercult.su.se. ..[color=blue]
                      > "jacob navia" <jacob.navia@ja cob.remcomp.fr> writes:
                      >[color=green]
                      > > C++ introduced an interesting feature (among others): operator
                      > > overloading.[/color]
                      >
                      > <explanation snipped>
                      >
                      > Just my 2c: operator (and function) overloading are about the only C++
                      > features I miss since I switched (back) to C.
                      >
                      > I think a number of replies to your post reflect the fear that, as
                      > soon as one starts to introduce new features, it will be impossible to
                      > stop and C will soon become as complex as C++. And I don't deny that
                      > there may be some truth in this claim.
                      >[/color]

                      Yes, and the experience of C++ should be a warning to all that start the same way:

                      KNOW WHEN TO STOP.

                      jacob


                      Comment

                      • Serve La

                        #12
                        Re: Operator overloading in C


                        "Zeljko Vrba" <mo.dor@fly.srk .fer.hr> wrote in message
                        news:slrnbj9uiq .b3.mordor@fly. srk.fer.hr...[color=blue]
                        > In article <bh2ohn$a1e$1@n ews-reader3.wanadoo .fr>, jacob navia wrote:[color=green]
                        > >
                        > > I say operator overloading is useful.
                        > >[/color]
                        > I didn't say it isn't. But then use C++. Nobody forces you to use classes,
                        > inheritance, access levels and other C++ novelties. You can use it just as
                        > an "enhanced C" using only features that you like/need/feel comfortable[/color]
                        with.

                        wrong. If you work with people that use C++, you can be sure that at some
                        point you have to use class, inheritance, access levels and other C++
                        novelties. This is only true if you work alone.



                        Comment

                        • Andre

                          #13
                          Re: Operator overloading in C



                          Serve La wrote:[color=blue]
                          > "Zeljko Vrba" <mo.dor@fly.srk .fer.hr> wrote in message
                          > news:slrnbj9uiq .b3.mordor@fly. srk.fer.hr...
                          >[color=green]
                          >>In article <bh2ohn$a1e$1@n ews-reader3.wanadoo .fr>, jacob navia wrote:
                          >>[color=darkred]
                          >>>I say operator overloading is useful.
                          >>>[/color]
                          >>
                          >>I didn't say it isn't. But then use C++. Nobody forces you to use classes,
                          >>inheritance , access levels and other C++ novelties. You can use it just as
                          >>an "enhanced C" using only features that you like/need/feel comfortable[/color]
                          >
                          > with.
                          >
                          > wrong. If you work with people that use C++, you can be sure that at some
                          > point you have to use class, inheritance, access levels and other C++
                          > novelties. This is only true if you work alone.[/color]

                          Exactly. If you're into component development and enjoy abstraction and
                          reusability, there's always going to be class, inheritance etc involved.
                          Also, what Zeljko said is also only true when you deliberately force an
                          application, that could benifit from Object orientation, to comply in a
                          non-OO fashion. Like Serve said, if you're working alone, you're the boss.

                          -Andre

                          Comment

                          • Dan Pop

                            #14
                            Re: Operator overloading in C

                            In <bh0vl6$nfk$1@n ews-reader5.wanadoo .fr> "jacob navia" <jacob.navia@ja cob.remcomp.fr> writes:
                            [color=blue]
                            >The syntax used is:
                            >
                            >TYPE operator+(TYPE a,TYPE b)
                            >{
                            >}
                            >
                            >When in your program you use
                            > TYPE a,b;
                            > ...
                            > a+b;
                            >Means that you call the operator function with those two numbers. This is similar to C++, but since
                            >there are no classes (C is not object oriented) everything is quite simple.
                            >
                            >Note that this is an extension compatible with C and it is important to know that the C standard
                            >doesn't forbid extensions. They should not introduce new keywords though.[/color]

                            The C standard requires a diagnostic for

                            TYPE operator+(TYPE a,TYPE b) {}

                            because it is syntactically invalid in C. The only extensions allowed
                            by the C standard are those based on undefined behaviour. So, to make
                            your extension compatible with standard C, replace "operator" by a an
                            identifier in the implementation name space, e.g. __operator__:

                            TYPE __operator__+(T YPE a,TYPE b) {}

                            invokes undefined behaviour (a reserved identifier not explicitly defined
                            by the C standard is used), therefore no diagnostic is needed, and your
                            compiler is free to silently overload the + operator as a result.

                            This technique of adding extensions is widely used by GNU C, so that
                            people can still use them if they invoke gcc in standard C mode. If your
                            compiler has also a non-standard C mode, you can use both "operator" and
                            "__operator __" in that mode. When invoked in standard C mode, "operator"
                            becomes an indentifier in the program namespace, though.
                            [color=blue]
                            >In my implementation it means that
                            >
                            >int operator = 6;
                            >
                            >still works as it should. No new keywords.[/color]

                            It doesn't matter, TYPE operator+(TYPE a,TYPE b) {} still requires a
                            diagnostic, according to the C syntax.
                            [color=blue]
                            >The operator symbol is recognized if (and only if):
                            >1) The level is global, i.e. not within a function definition.
                            >2) It is followed by one of the defined operator symbols (+, -, etc)
                            >3) This must be followed by a normal function definition. The name of this function is the sequence
                            >of types in the argument list, the "signature" in technical terms.[/color]

                            That's wrong, again. Such names are in the program name space, you can't
                            define them by magic. Use the __ prefix to fix the issue. But you have
                            an additional problem: if the function signature is the function name,
                            how do you distiguish between operators with identical signatures, e.g.

                            TYPE operator+(TYPE a,TYPE b) {...}
                            TYPE operator-(TYPE a,TYPE b) {...}
                            [color=blue]
                            >Is this useful?[/color]

                            The answer is mostly an issue of religion. For better acceptance, don't
                            allow TYPE to be any of the C scalar types.

                            Dan
                            --
                            Dan Pop
                            DESY Zeuthen, RZ group
                            Email: Dan.Pop@ifh.de

                            Comment

                            • jacob navia

                              #15
                              Re: Operator overloading in C


                              "Dan Pop" <Dan.Pop@cern.c h> wrote in message news:bh88o1$qpj $6@sunnews.cern .ch...[color=blue]
                              > In <bh0vl6$nfk$1@n ews-reader5.wanadoo .fr> "jacob navia" <jacob.navia@ja cob.remcomp.fr> writes:
                              >[/color]
                              [snip][color=blue]
                              > But you have
                              > an additional problem: if the function signature is the function name,
                              > how do you distiguish between operators with identical signatures, e.g.
                              >
                              > TYPE operator+(TYPE a,TYPE b) {...}
                              > TYPE operator-(TYPE a,TYPE b) {...}
                              >[/color]

                              The name of the first operator is:

                              _operator_plus_ TYPE_TYPE
                              the second is
                              _operator_minus _TYPE_TYPE


                              The biggest problem I had is the pointer problem.
                              The operator + can't be defined with types like

                              TYPE operator+(TYPE a,int b)

                              since the operation a+7

                              is used in the language already (means a + 7 *sizeof(a)) and would
                              clash with standard C. This restriction is applied to all operators.

                              In any way, the compiler has several flags that make it reject any
                              non-standard grammar like this (-ansic) or even go back to ansi 89.

                              jacob


                              Comment

                              Working...