diff options
author | Jordan Rose <jordan_rose@apple.com> | 2016-11-07 20:34:16 +0000 |
---|---|---|
committer | Jordan Rose <jordan_rose@apple.com> | 2016-11-07 20:34:16 +0000 |
commit | d75c17840807eb766ddaece87f32a9bd083093fa (patch) | |
tree | b49a7608709dd8b2edde359b0fab5e1a7950efe3 /include | |
parent | 7e34ddb6ca83485ff0f21b847e4ddb4230422f6b (diff) | |
download | llvm-d75c17840807eb766ddaece87f32a9bd083093fa.tar.gz |
Disallow StringRef assignment from temporary std::strings.
Similar to r283798, this prevents accidentally referring to temporary
storage that goes out of scope by the end of the statement:
someStringRef = getStringByValue();
someStringRef = (Twine("-") + otherString).str();
Note that once again the constructor still has this problem:
StringRef someStringRef = getStringByValue();
because once again we occasionally rely on this in calls:
takesStringRef(getStringByValue());
takesStringRef(Twine("-") + otherString);
Still, it's a step.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286139 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/llvm/ADT/StringRef.h | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index cc5a5795eeb2..97359dd2630b 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -226,6 +226,15 @@ namespace llvm { return Data[Index]; } + /// Disallow accidental assignment from a temporary std::string. + /// + /// The declaration here is extra complicated so that `stringRef = {}` + /// and `stringRef = "abc"` continue to select the move assignment operator. + template <typename T> + typename std::enable_if<std::is_same<T, std::string>::value, + StringRef>::type & + operator=(T &&Str) = delete; + /// @} /// @name Type Conversions /// @{ |