Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit b2fa9f7

Browse files
committed
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
1 parent 07eb96d commit b2fa9f7

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

include/llvm/ADT/StringRef.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ namespace llvm {
226226
return Data[Index];
227227
}
228228

229+
/// Disallow accidental assignment from a temporary std::string.
230+
///
231+
/// The declaration here is extra complicated so that `stringRef = {}`
232+
/// and `stringRef = "abc"` continue to select the move assignment operator.
233+
template <typename T>
234+
typename std::enable_if<std::is_same<T, std::string>::value,
235+
StringRef>::type &
236+
operator=(T &&Str) = delete;
237+
229238
/// @}
230239
/// @name Type Conversions
231240
/// @{

0 commit comments

Comments
 (0)