using swap to make assignment operator exception safe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • George2
    New Member
    • Dec 2007
    • 200

    using swap to make assignment operator exception safe

    Hello everyone,


    The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant).

    It used a temporary object "temp" in this sample, and assignment is made on a to temp ar first. Even if there is exception, the current this object's state is not corrupted.

    My question is, the pattern works only if there is no exception thrown by swap function. If there are exception in swap function, the state of current object instance may still be corrupted (swap may invoke the assignment operator of member variables). Is my understanding correct?

    Code:
    class A;
    A& A::operator= (const A& a)
    {
        A temp;
        temp = a; // exception may be thrown
        swap (*this, temp);
        return *this;
    }

    thanks in advance,
    George
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Yep. You would have to try the swap and if an exception were thrown, then you would throw onw yourself to terminate the current object.

    Comment

    • George2
      New Member
      • Dec 2007
      • 200

      #3
      Thanks for your comments, weaknessforcats .


      My question is not using swap itself, but whether or not the pattern I put above only works if swap is exception safe.

      Originally posted by weaknessforcats
      Yep. You would have to try the swap and if an exception were thrown, then you would throw onw yourself to terminate the current object.

      regards,
      George

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        And how would you know if the swap is exception safe???

        Comment

        • George2
          New Member
          • Dec 2007
          • 200

          #5
          Hi weaknessforcats ,


          I mean it (swap does not throw exception) should be assumed and as a prerequisite to use the pattern I mentioned in original question to make an exception safe assignment operator.

          If swap throws exception, the pattern is not exception safe.

          Originally posted by weaknessforcats
          And how would you know if the swap is exception safe???

          regards,
          George

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by George2
            should be assumed
            There is a crash coming....

            Comment

            • George2
              New Member
              • Dec 2007
              • 200

              #7
              Sorry weaknessforcats ,


              What do you mean crash? What is crashed and under what situation? :-)

              Originally posted by weaknessforcats
              There is a crash coming....

              have a good weekend,
              George

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                If you assume there are no exceptions and one comes along, you won't catch it. It will float roght up top the OS where your process dies with an unhandled exception.

                Comment

                • George2
                  New Member
                  • Dec 2007
                  • 200

                  #9
                  Hi weaknessforcats ,


                  If swap is implemented in user defined types, we can surely know whether or not it throws any exceptions or not. Right? :-)

                  So, no unknown and mysterious things.

                  Originally posted by weaknessforcats
                  If you assume there are no exceptions and one comes along, you won't catch it. It will float roght up top the OS where your process dies with an unhandled exception.

                  regards,
                  George

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Nope.

                    There's to way to know for sure that there isn't a throw in there somewhere.

                    Comment

                    • George2
                      New Member
                      • Dec 2007
                      • 200

                      #11
                      Hi weaknessforcats ,


                      1. If the code is written by you, you can definietely find the answers.

                      2. If not, you can get from the API document.

                      Why you can not know what exceptions will be thrown?

                      Originally posted by weaknessforcats
                      Nope.

                      There's to way to know for sure that there isn't a throw in there somewhere.

                      regards,
                      George

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        An API document won't detect exceptions. You are not compiling the document but instead are compiling someone's code. That person may or may not have told the API writer about an exception. Or, the exception as added later and not put in the document.

                        Besides, of you use this approach, you will need top reread the API document every time it is updated to see if anything changes- and even then you still could be lied to. BTW: The Windows SDK is updated monthly. Happy reading.

                        You can't even tell with your own code. Why? because your code that does not throw may call a function written by someone else that does. So checking your code for throws won't cut it either.

                        The key here is assume. If you assume no exceptions, then you may be in trouble. Maybe not. It's better to assume the worst and have a plan for it. Of course, that makes more work.

                        You can't even get around this using C, which has no exceptions, because Microsoft has implemented structured exceptions in their C code and you have to be able to handle those as well.

                        Comment

                        • George2
                          New Member
                          • Dec 2007
                          • 200

                          #13
                          Thanks weaknessforcats ,


                          Originally posted by weaknessforcats
                          An API document won't detect exceptions. You are not compiling the document but instead are compiling someone's code. That person may or may not have told the API writer about an exception. Or, the exception as added later and not put in the document.
                          For Windows native STL API (I mean the ones developed by Microsoft), whether or not exception will be thrown is 100 sure. And we can rely on those. Agree? :-)


                          regards,
                          George

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            Originally posted by George2
                            For Windows native STL API (I mean the ones developed by Microsoft), whether or not exception will be thrown is 100 sure. And we can rely on those. Agree? :-)
                            Nope. Check out structured exceptions.

                            Comment

                            • George2
                              New Member
                              • Dec 2007
                              • 200

                              #15
                              Structured exceptions are H/W related.


                              Originally posted by weaknessforcats
                              Nope. Check out structured exceptions.

                              regards,
                              George

                              Comment

                              Working...