From 0bff4f21d5d0fb0bd7cc723dc8174acdaf27ffb1 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Mon, 17 Aug 2015 20:56:40 +0200 Subject: [PATCH] Record References: fix copy and assignment handling not entirely sure about the design, but lets try this approach: they can be "cloned" and likewise move-assigned, but we do not allow the regular assignment, because this would enable to use references like pointers (what we deliberately do not want) --- src/lib/diff/record.hpp | 15 ++++++++++++++- .../diff/generic-record-representation-test.cpp | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lib/diff/record.hpp b/src/lib/diff/record.hpp index 8cf3e3411..8531695f9 100644 --- a/src/lib/diff/record.hpp +++ b/src/lib/diff/record.hpp @@ -514,7 +514,20 @@ namespace diff{ /** prevent moving into black hole */ RecordRef(Target&&) = delete; - // standard copy operations acceptable + RecordRef(RecordRef const&) = default; + RecordRef(RecordRef &&) = default; + + /** references can not be reassigned */ + RecordRef& operator= (RecordRef const&) = delete; + RecordRef& operator= (RecordRef &) = delete; + + /** assignment is not allowed, but moving is */ + RecordRef& + operator= (RecordRef &&o) + { + std::swap(record_, o.record_); + return *this; + } explicit diff --git a/tests/library/diff/generic-record-representation-test.cpp b/tests/library/diff/generic-record-representation-test.cpp index 1eac8e22b..1c65a8c62 100644 --- a/tests/library/diff/generic-record-representation-test.cpp +++ b/tests/library/diff/generic-record-representation-test.cpp @@ -326,12 +326,13 @@ namespace test{ CHECK ("🌰" == oor.getType()); CHECK (oor.get("♄") == "saturn"); - // are copyable and assignable + // are copyable but not reassignable RecordRef r2 = ref; CHECK (r2); CHECK (r2.get() == ref.get()); CHECK (!isSameObject (r2, ref)); + // but references are move-assignable empty = std::move(r2); CHECK (empty); CHECK (!r2);