operator overloading for primative types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PengYu.UT@gmail.com

    #1

    operator overloading for primative types

    Hi,

    It seems that C++ does not allow overloading operators for primative
    types, e.g. int, double. I'm wondering whether it is ture or there is
    some walk-around?

    Thanks,
    Peng

    #include <iostream>
    #include <complex>

    namespace my
    {
    int operator/(int x, int y) {
    if(x>0)
    return x/y;
    else
    return x/y - 1;
    }

    void test(int x, int y) {
    std::cout << x << "/" << y << " " << x/y << std::endl;
    }

    }

    int main() {
    my::test(10, 3);
    my::test(-10, 3);
    my::test(10, -3);
    my::test(-10, -3);
    }

  • Kai-Uwe Bux

    #2
    Re: operator overloading for primative types

    PengYu.UT@gmail .com wrote:
    Hi,
    >
    It seems that C++ does not allow overloading operators for primative
    types, e.g. int, double. I'm wondering whether it is ture
    It is true. See clause [13.3.1.2/1]


    If no operand of an operator in an expression has a type that is a class
    or an enumeration, the operator is assumed to be a built-in operator and
    interpreted according to clause 5.
    or there is some walk-around?
    I don't know of any that work transparently. You have to rig your operands
    so that at least one of them is not of built-in type.


    Best

    Kai-Uwe Bux


    Comment

    • Ian Collins

      #3
      Re: operator overloading for primative types

      PengYu.UT@gmail .com wrote:
      Hi,
      >
      It seems that C++ does not allow overloading operators for primative
      types, e.g. int, double. I'm wondering whether it is ture or there is
      some walk-around?
      >
      Luckily you can't, doing so would produce code that doesn't behave as
      others would expect it to, a nightmare to maintain.

      If you want a type with specialised behavior, wrap it in a class with
      the appropriate operators.

      --
      Ian Collins.

      Comment

      • coal@mailvault.com

        #4
        Re: operator overloading for primative types

        On Sep 26, 10:51 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        PengYu...@gmail .com wrote:
        Hi,
        >
        It seems that C++ does not allow overloading operators for primative
        types, e.g. int, double. I'm wondering whether it is ture or there is
        some walk-around?
        >
        Luckily you can't, doing so would produce code that doesn't behave as
        others would expect it to, a nightmare to maintain.
        I agree. Some people are advocating something similar should be done
        by introducing a std::intrusive: :list type. I think it would be
        better to make it ilist -
        http://tinyurl.com/2q5p34


        Brian Wood
        Ebenezer Enterprises


        Comment

        • Me

          #5
          Re: operator overloading for primative types

          On Thu, 27 Sep 2007 15:51:26 +1200, Noone wrote:
          PengYu.UT@gmail .com wrote:
          >Hi,
          >>
          >It seems that C++ does not allow overloading operators for primative
          >types, e.g. int, double. I'm wondering whether it is ture or there is
          >some walk-around?
          >>
          Luckily you can't, doing so would produce code that doesn't behave as
          others would expect it to, a nightmare to maintain.
          >
          If you want a type with specialised behavior, wrap it in a class with
          the appropriate operators.


          Yes. I believe in the Macrosloth world they call that boxing:
          encapsulating a primitive type in an object so that it can be manipulated
          by other classes as a generic (Object), with whatever behavior the
          programmer wants to implement. I pass no judgement on whether this is a
          good thing or a bad thing. :^)

          Comment

          • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

            #6
            Re: operator overloading for primative types

            On 2007-09-28 00:07, Me wrote:
            On Thu, 27 Sep 2007 15:51:26 +1200, Noone wrote:
            >
            >PengYu.UT@gmail .com wrote:
            >>Hi,
            >>>
            >>It seems that C++ does not allow overloading operators for primative
            >>types, e.g. int, double. I'm wondering whether it is ture or there is
            >>some walk-around?
            >>>
            >Luckily you can't, doing so would produce code that doesn't behave as
            >others would expect it to, a nightmare to maintain.
            >>
            >If you want a type with specialised behavior, wrap it in a class with
            >the appropriate operators.
            >
            >
            >
            Yes. I believe in the Macrosloth world they call that boxing:
            encapsulating a primitive type in an object so that it can be manipulated
            by other classes as a generic (Object), with whatever behavior the
            programmer wants to implement. I pass no judgement on whether this is a
            good thing or a bad thing. :^)
            [Off topic]
            The goal of boxing is to be able to place a value type on the managed
            heap, so that it can be accessed by methods (and other things) that only
            accept reference types. The equivalent in C++ would be something like this:

            int a;
            int* boxedA = new int(a);

            One important thing to notice is that the boxed object and the non-boxed
            are two separate objects with no connection what so ever (except having
            the same value).

            Specifically you can not take an int, box it, and then apply some user
            defined operators on it. To do that you would have to create a value
            type with overloaded operators and then box that. Which would be the
            same as creating a new type in C++ and overloading its operators.

            [/Off topic]

            --
            Erik Wikström

            Comment

            • Juha Nieminen

              #7
              Re: operator overloading for primative types

              PengYu.UT@gmail .com wrote:
              primative
              What's that?

              Comment

              • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

                #8
                Re: operator overloading for primative types

                On 2007-09-29 09:33, Juha Nieminen wrote:
                PengYu.UT@gmail .com wrote:
                >primative
                >
                What's that?
                It means that either your English is not good enough to understand what
                the OP meant to say (in which case you could have asked in a more polite
                way), or you did understand it and you are just being a jerk. I am
                hoping it is the first one.

                --
                Erik Wikström

                Comment

                • Juha Nieminen

                  #9
                  Re: operator overloading for primative types

                  Erik Wikström wrote:
                  or you did understand it and you are just being a jerk.
                  In this society in which we live there's a rule of conduct which I
                  find the most peculiar and idiotic: It's considered impolite to remark
                  about other people's grammatical/spelling mistakes.

                  IMO this is the stupidest rule ever. If nobody tells you that you are
                  consistently writing something in the wrong way, how can you know that
                  you are writing in the wrong way? How can you learn the language if
                  nobody tells you what you are doing wrong? By being "polite" people are
                  only perpetuating the bad grammar instead of helping him learning.

                  There's at least one positive outcome in "being a jerk": If I myself
                  make a grammatical/spelling mistake, somebody is very likely to tell me
                  about it. This helps me learning to write better.

                  Comment

                  • Ian Collins

                    #10
                    Re: operator overloading for primative types

                    Juha Nieminen wrote:
                    >
                    There's at least one positive outcome in "being a jerk": If I myself
                    make a grammatical/spelling mistake, somebody is very likely to tell me
                    about it. This helps me learning to write better.
                    You asked for it - "I myself" is a tautology.

                    --
                    Ian Collins.

                    Comment

                    • Kai-Uwe Bux

                      #11
                      Re: operator overloading for primative types

                      Juha Nieminen wrote:
                      Erik Wikström wrote:
                      >or you did understand it and you are just being a jerk.
                      >
                      In this society in which we live there's a rule of conduct which I
                      find the most peculiar and idiotic: It's considered impolite to remark
                      about other people's grammatical/spelling mistakes.
                      >
                      IMO this is the stupidest rule ever. If nobody tells you that you are
                      consistently writing something in the wrong way, how can you know that
                      you are writing in the wrong way? How can you learn the language if
                      nobody tells you what you are doing wrong? By being "polite" people are
                      only perpetuating the bad grammar instead of helping him learning.
                      >
                      There's at least one positive outcome in "being a jerk": If I myself
                      make a grammatical/spelling mistake, somebody is very likely to tell me
                      about it. This helps me learning to write better.
                      a) I know exactly what you are talking about. I am not a native speaker of
                      English living in the USA. Most people do not correct my mistakes, and it
                      makes it really hard to improve. Fortunately, I have a few friends whom I
                      was able to convice otherwise.

                      b) What made you appear like a jerk is not that you gave a correction; it's
                      that you chose to do so by droping into a conversation to which you had
                      nothing else to add.


                      Best

                      Kai-Uwe Bux

                      Comment

                      • Kai-Uwe Bux

                        #12
                        Re: operator overloading for primative types

                        Alf P. Steinbach wrote:
                        * Kai-Uwe Bux:
                        >>
                        >a) I know exactly what you are talking about. I am not a native speaker
                        >of English living in the USA. Most people do not correct my mistakes, and
                        >it makes it really hard to improve. Fortunately, I have a few friends
                        >whom I was able to convice otherwise.
                        ^^^^^^^
                        >
                        Heya! :-)
                        >
                        >
                        >
                        >b) What made you appear like a jerk is not that you gave a correction;
                        >it's that you chose to do so by droping into a conversation to which you
                        >had
                        ^^^^^^^
                        >
                        :-) :-) :-)
                        Thanks.

                        >nothing else to add.
                        What an interesting instance of the liars paradox: had you left out this
                        sentence, it would have been true. :-)


                        Best

                        Kai-Uwe Bux

                        Comment

                        • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

                          #13
                          Re: operator overloading for primative types

                          On 2007-09-30 10:42, Juha Nieminen wrote:
                          Erik Wikström wrote:
                          >or you did understand it and you are just being a jerk.
                          >
                          In this society in which we live there's a rule of conduct which I
                          find the most peculiar and idiotic: It's considered impolite to remark
                          about other people's grammatical/spelling mistakes.
                          >
                          IMO this is the stupidest rule ever. If nobody tells you that you are
                          consistently writing something in the wrong way, how can you know that
                          you are writing in the wrong way? How can you learn the language if
                          nobody tells you what you are doing wrong? By being "polite" people are
                          only perpetuating the bad grammar instead of helping him learning.
                          >
                          There's at least one positive outcome in "being a jerk": If I myself
                          make a grammatical/spelling mistake, somebody is very likely to tell me
                          about it. This helps me learning to write better.
                          I have nothing against pointing out people's spelling or grammar
                          mistakes, it was just your way of doing it and the fact that it was your
                          only contribution to the discussion. You could have just pointed out
                          that the OP spelt it wrong and then answered the question. Or just
                          answered the question making sure to include the correct spelling in
                          your reply and trust that the OP was smart enough to pick it up.

                          --
                          Erik Wikström

                          Comment

                          • osmium

                            #14
                            Re: operator overloading for primative types

                            "Juha Nieminen" writes:
                            There's at least one positive outcome in "being a jerk": If I myself
                            make a grammatical/spelling mistake, somebody is very likely to tell me
                            about it. This helps me learning to write better.
                            I think it should be "This helps me learn to be a better writer".

                            I am not a grammarian but I stayed at a Holiday Inn.


                            Comment

                            • Barry

                              #15
                              Re: operator overloading for primative types

                              Alf P. Steinbach wrote:
                              * Kai-Uwe Bux:
                              >>
                              >a) I know exactly what you are talking about. I am not a native
                              >speaker of
                              >English living in the USA. Most people do not correct my mistakes, and it
                              >makes it really hard to improve. Fortunately, I have a few friends whom I
                              >was able to convice otherwise.
                              ^^^^^^^
                              >
                              Heya! :-)
                              >
                              >
                              >
                              >b) What made you appear like a jerk is not that you gave a correction;
                              >it's
                              >that you chose to do so by droping into a conversation to which you had
                              ^^^^^^^
                              >
                              :-) :-) :-)
                              >
                              >
                              >nothing else to add.
                              >
                              Cheers,
                              >
                              - Alf (speling master)
                              Nah, spelling master
                              ;-)
                              >

                              Comment

                              Working...