[cfe-dev] Unintuitive APSInt assignment

Roman Popov via cfe-dev cfe-dev at lists.llvm.org
Thu Aug 2 14:41:24 PDT 2018


Hello,
I'm working on C++ interpreter that uses APSInt type to represent integers.
I've just spent an hour debugging a bug the cause of which was unintuitive
APSInt behavior / method signature.

APSInt assignments are declared as follows:

class LLVM_NODISCARD APSInt : public APInt {
...
  APSInt &operator=(APInt RHS) {
*    // Retain our current sign.*
    APInt::operator=(std::move(RHS));
    return *this;
  }

  APSInt &operator=(uint64_t RHS) {
*    // Retain our current sign.*
    APInt::operator=(RHS);
    return *this;
  }
...
};

By looking into header one may think that assignment *always retains the
sign. *But this is not true, because automatically generated copy
assignment will copy sign from RHS.

-Roman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180802/ab8019ef/attachment.html>


More information about the cfe-dev mailing list