From: Frits van Bommel Date: Mon, 18 Jul 2011 11:58:53 +0000 (+0000) Subject: Introduce the 'makeArrayRef(...)' family of functions, which fills a similar role... X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=c48e1ef0e22b4113dd4dd48c5b170a19fe4d0188;p=oota-llvm.git Introduce the 'makeArrayRef(...)' family of functions, which fills a similar role for ArrayRef<> as std::make_pair() fills for std::pair<>: they return the right instantiation of ArrayRef based on the types of the parameters. They mostly mirror the ArrayRef constructors, with two exceptions: * There's no function mirroring the default constructor because it wouldn't have any parameters to deduce the right ArrayRef from. * There's an explicit SmallVector overload in addition to the SmallVectorImpl overload. Without it, the single-element overload would try to create an ArrayRef > because it's a better match according to the overloading rules. (And both overloads are used in the current tree, so neither is redundant) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135389 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/ArrayRef.h b/include/llvm/ADT/ArrayRef.h index 6db866e8c70..8eb715546e5 100644 --- a/include/llvm/ADT/ArrayRef.h +++ b/include/llvm/ADT/ArrayRef.h @@ -147,7 +147,53 @@ namespace llvm { /// @} }; - + + /// @name ArrayRef Convenience constructors + /// @{ + + /// Construct an ArrayRef from a single element. + template + ArrayRef makeArrayRef(const T &OneElt) { + return OneElt; + } + + /// Construct an ArrayRef from a pointer and length. + template + ArrayRef makeArrayRef(const T *data, size_t length) { + return ArrayRef(data, length); + } + + /// Construct an ArrayRef from a range. + template + ArrayRef makeArrayRef(const T *begin, const T *end) { + return ArrayRef(begin, end); + } + + /// Construct an ArrayRef from a SmallVector. + template + ArrayRef makeArrayRef(const SmallVectorImpl &Vec) { + return Vec; + } + + /// Construct an ArrayRef from a SmallVector. + template + ArrayRef makeArrayRef(const SmallVector &Vec) { + return Vec; + } + + /// Construct an ArrayRef from a std::vector. + template + ArrayRef makeArrayRef(const std::vector &Vec) { + return Vec; + } + + /// Construct an ArrayRef from a C array. + template + ArrayRef makeArrayRef(const T (&Arr)[N]) { + return Arr; + } + + /// @} /// @name ArrayRef Comparison Operators /// @{