Disallow assignment to rvalue Range objects (and StringPiece in particular)
authorAndrei Alexandrescu <aalexandre@fb.com>
Wed, 4 Feb 2015 19:57:51 +0000 (11:57 -0800)
committerSara Golemon <sgolemon@fb.com>
Wed, 11 Feb 2015 02:01:59 +0000 (18:01 -0800)
Summary:
Before this diff the code below compiled and did absolutely nothing of interest:

StringPiece fun();
...
fun() = "hello";

i.e. assignment to an rvalue range was allowed. Such code is almost always, if not always, in error. This diff fixes that.

Test Plan: ran unittests

Reviewed By: ldbrandy@fb.com

Subscribers: mpawlowski, net-systems@, folly-diffs@, yfeldblum

FB internal diff: D1825360

Signature: t1:1825360:1423097817:fdaaf893cd1abbe71dc857a315df7c45cb6a0bd0

folly/Range.h

index 1d904faf8350419048a0a2c5c36233f6447b5c47..da27ac61f8a93661bd8467a0de22056ba4534946 100644 (file)
@@ -184,6 +184,9 @@ public:
   constexpr Range() : b_(), e_() {
   }
 
+  constexpr Range(const Range&) = default;
+  constexpr Range(Range&&) = default;
+
 public:
   // Works for all iterators
   constexpr Range(Iter start, Iter end) : b_(start), e_(end) {
@@ -322,6 +325,9 @@ public:
       e_(other.end()) {
   }
 
+  Range& operator=(const Range& rhs) & = default;
+  Range& operator=(Range&& rhs) & = default;
+
   void clear() {
     b_ = Iter();
     e_ = Iter();