r202755 - [C++11] Simplify compare operators with std::tie.

Sean Silva silvas at purdue.edu
Mon Mar 3 14:58:37 PST 2014


This is my new favorite "tiny but useful" C++11 trick.

-- Sean Silva


On Mon, Mar 3, 2014 at 3:26 PM, Benjamin Kramer <benny.kra at googlemail.com>wrote:

> Author: d0k
> Date: Mon Mar  3 14:26:46 2014
> New Revision: 202755
>
> URL: http://llvm.org/viewvc/llvm-project?rev=202755&view=rev
> Log:
> [C++11] Simplify compare operators with std::tie.
>
> No functionality change.
>
> Modified:
>     cfe/trunk/include/clang/AST/VTableBuilder.h
>     cfe/trunk/include/clang/Basic/VersionTuple.h
>     cfe/trunk/include/clang/Edit/FileOffset.h
>     cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h
>     cfe/trunk/lib/AST/VTableBuilder.cpp
>     cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp
>     cfe/trunk/lib/CodeGen/CodeGenModule.h
>     cfe/trunk/lib/StaticAnalyzer/Core/BlockCounter.cpp
>
> Modified: cfe/trunk/include/clang/AST/VTableBuilder.h
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/VTableBuilder.h?rev=202755&r1=202754&r2=202755&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/AST/VTableBuilder.h (original)
> +++ cfe/trunk/include/clang/AST/VTableBuilder.h Mon Mar  3 14:26:46 2014
> @@ -471,11 +471,8 @@ public:
>          assert(VBase != other.VBase);
>          return VBTableIndex < other.VBTableIndex;
>        }
> -      if (VFPtrOffset != other.VFPtrOffset)
> -        return VFPtrOffset < other.VFPtrOffset;
> -      if (Index != other.Index)
> -        return Index < other.Index;
> -      return false;
> +      return std::tie(VFPtrOffset, Index) <
> +             std::tie(other.VFPtrOffset, other.Index);
>      }
>    };
>
>
> Modified: cfe/trunk/include/clang/Basic/VersionTuple.h
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/VersionTuple.h?rev=202755&r1=202754&r2=202755&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/VersionTuple.h (original)
> +++ cfe/trunk/include/clang/Basic/VersionTuple.h Mon Mar  3 14:26:46 2014
> @@ -87,13 +87,8 @@ public:
>    /// If not provided, minor and subminor version numbers are considered
> to be
>    /// zero.
>    friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
> -    if (X.Major != Y.Major)
> -      return X.Major < Y.Major;
> -
> -    if (X.Minor != Y.Minor)
> -      return X.Minor < Y.Minor;
> -
> -    return X.Subminor < Y.Subminor;
> +    return std::tie(X.Major, X.Minor, X.Subminor) <
> +           std::tie(Y.Major, Y.Minor, Y.Subminor);
>    }
>
>    /// \brief Determine whether one version number follows another.
>
> Modified: cfe/trunk/include/clang/Edit/FileOffset.h
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Edit/FileOffset.h?rev=202755&r1=202754&r2=202755&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Edit/FileOffset.h (original)
> +++ cfe/trunk/include/clang/Edit/FileOffset.h Mon Mar  3 14:26:46 2014
> @@ -41,20 +41,16 @@ public:
>      return !(LHS == RHS);
>    }
>    friend bool operator<(FileOffset LHS, FileOffset RHS) {
> -    if (LHS.FID != RHS.FID)
> -      return LHS.FID < RHS.FID;
> -    return LHS.Offs < RHS.Offs;
> +    return std::tie(LHS.FID, LHS.Offs) < std::tie(RHS.FID, RHS.Offs);
>    }
>    friend bool operator>(FileOffset LHS, FileOffset RHS) {
> -    if (LHS.FID != RHS.FID)
> -      return LHS.FID > RHS.FID;
> -    return LHS.Offs > RHS.Offs;
> +    return RHS < LHS;
>    }
>    friend bool operator>=(FileOffset LHS, FileOffset RHS) {
> -    return LHS > RHS || LHS == RHS;
> +    return !(LHS < RHS);
>    }
>    friend bool operator<=(FileOffset LHS, FileOffset RHS) {
> -    return LHS < RHS || LHS == RHS;
> +    return !(RHS < LHS);
>    }
>  };
>
>
> Modified:
> cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h?rev=202755&r1=202754&r2=202755&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h
> (original)
> +++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h
> Mon Mar  3 14:26:46 2014
> @@ -97,13 +97,8 @@ public:
>    /// Unsigned integers are considered to be better conversion types than
>    /// signed integers of the same width.
>    bool operator<(const APSIntType &Other) const {
> -    if (BitWidth < Other.BitWidth)
> -      return true;
> -    if (BitWidth > Other.BitWidth)
> -      return false;
> -    if (!IsUnsigned && Other.IsUnsigned)
> -      return true;
> -    return false;
> +    return std::tie(BitWidth, IsUnsigned) <
> +           std::tie(Other.BitWidth, Other.IsUnsigned);
>    }
>  };
>
>
> Modified: cfe/trunk/lib/AST/VTableBuilder.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/VTableBuilder.cpp?rev=202755&r1=202754&r2=202755&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/AST/VTableBuilder.cpp (original)
> +++ cfe/trunk/lib/AST/VTableBuilder.cpp Mon Mar  3 14:26:46 2014
> @@ -2156,10 +2156,7 @@ void ItaniumVTableBuilder::dumpLayout(ra
>        std::sort(ThunksVector.begin(), ThunksVector.end(),
>                  [](const ThunkInfo &LHS, const ThunkInfo &RHS) {
>          assert(LHS.Method == 0 && RHS.Method == 0);
> -
> -        if (LHS.This != RHS.This)
> -          return LHS.This < RHS.This;
> -        return LHS.Return < RHS.Return;
> +        return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This,
> RHS.Return);
>        });
>
>        Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
> @@ -3169,9 +3166,7 @@ void VFTableBuilder::dumpLayout(raw_ostr
>                         [](const ThunkInfo &LHS, const ThunkInfo &RHS) {
>          // Keep different thunks with the same adjustments in the order
> they
>          // were put into the vector.
> -        if (LHS.This != RHS.This)
> -          return LHS.This < RHS.This;
> -        return LHS.Return < RHS.Return;
> +        return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This,
> RHS.Return);
>        });
>
>        Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
>
> Modified: cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp?rev=202755&r1=202754&r2=202755&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp (original)
> +++ cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp Mon Mar  3 14:26:46 2014
> @@ -58,11 +58,8 @@ struct MatchKey {
>    BoundNodesTreeBuilder BoundNodes;
>
>    bool operator<(const MatchKey &Other) const {
> -    if (MatcherID != Other.MatcherID)
> -      return MatcherID < Other.MatcherID;
> -    if (Node != Other.Node)
> -      return Node < Other.Node;
> -    return BoundNodes < Other.BoundNodes;
> +    return std::tie(MatcherID, Node, BoundNodes) <
> +           std::tie(Other.MatcherID, Other.Node, Other.BoundNodes);
>    }
>  };
>
>
> Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=202755&r1=202754&r2=202755&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenModule.h Mon Mar  3 14:26:46 2014
> @@ -99,10 +99,8 @@ namespace CodeGen {
>      }
>
>      bool operator<(const OrderGlobalInits &RHS) const {
> -      if (priority < RHS.priority)
> -        return true;
> -
> -      return priority == RHS.priority && lex_order < RHS.lex_order;
> +      return std::tie(priority, lex_order) <
> +             std::tie(RHS.priority, RHS.lex_order);
>      }
>    };
>
>
> Modified: cfe/trunk/lib/StaticAnalyzer/Core/BlockCounter.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/BlockCounter.cpp?rev=202755&r1=202754&r2=202755&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/StaticAnalyzer/Core/BlockCounter.cpp (original)
> +++ cfe/trunk/lib/StaticAnalyzer/Core/BlockCounter.cpp Mon Mar  3 14:26:46
> 2014
> @@ -34,8 +34,7 @@ public:
>    }
>
>    bool operator<(const CountKey &RHS) const {
> -    return (CallSite == RHS.CallSite) ? (BlockID < RHS.BlockID)
> -                                      : (CallSite < RHS.CallSite);
> +    return std::tie(CallSite, BlockID) < std::tie(RHS.CallSite,
> RHS.BlockID);
>    }
>
>    void Profile(llvm::FoldingSetNodeID &ID) const {
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140303/40dcacb8/attachment.html>


More information about the cfe-commits mailing list