Skip to content

Commit 45cd949

Browse files
Billy Donahuecdunn2001
Billy Donahue
authored andcommitted
Switch to copy-and-swap idiom for operator=.
This allows the compiler to elide a copy when rhs is a temporary.
1 parent 236db83 commit 45cd949

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

Diff for: include/json/value.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class JSON_API Value {
171171
CZString(const char *cstr, DuplicationPolicy allocate);
172172
CZString(const CZString &other);
173173
~CZString();
174-
CZString &operator=(const CZString &other);
174+
CZString &operator=(CZString other);
175175
bool operator<(const CZString &other) const;
176176
bool operator==(const CZString &other) const;
177177
ArrayIndex index() const;
@@ -238,7 +238,7 @@ Json::Value obj_value(Json::objectValue); // {}
238238
Value(const Value &other);
239239
~Value();
240240

241-
Value &operator=(const Value &other);
241+
Value &operator=(Value other);
242242
/// Swap values.
243243
/// \note Currently, comments are intentionally not swapped, for
244244
/// both logic and efficiency.
@@ -681,7 +681,7 @@ class JSON_API ValueInternalMap {
681681

682682
ValueInternalMap();
683683
ValueInternalMap(const ValueInternalMap &other);
684-
ValueInternalMap &operator=(const ValueInternalMap &other);
684+
ValueInternalMap &operator=(ValueInternalMap other);
685685
~ValueInternalMap();
686686

687687
void swap(ValueInternalMap &other);
@@ -775,7 +775,7 @@ class JSON_API ValueInternalArray {
775775

776776
ValueInternalArray();
777777
ValueInternalArray(const ValueInternalArray &other);
778-
ValueInternalArray &operator=(const ValueInternalArray &other);
778+
ValueInternalArray &operator=(ValueInternalArray other);
779779
~ValueInternalArray();
780780
void swap(ValueInternalArray &other);
781781

Diff for: src/lib_json/json_internalarray.inl

+2-3
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,9 @@ ValueInternalArray::ValueInternalArray( const ValueInternalArray &other )
280280

281281

282282
ValueInternalArray &
283-
ValueInternalArray::operator =( const ValueInternalArray &other )
283+
ValueInternalArray::operator=(ValueInternalArray other)
284284
{
285-
ValueInternalArray temp( other );
286-
swap( temp );
285+
swap(other);
287286
return *this;
288287
}
289288

Diff for: src/lib_json/json_internalmap.inl

+2-3
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,9 @@ ValueInternalMap::ValueInternalMap( const ValueInternalMap &other )
196196

197197

198198
ValueInternalMap &
199-
ValueInternalMap::operator =( const ValueInternalMap &other )
199+
ValueInternalMap::operator=(ValueInternalMap other)
200200
{
201-
ValueInternalMap dummy( other );
202-
swap( dummy );
201+
swap(other);
203202
return *this;
204203
}
205204

Diff for: src/lib_json/json_value.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,8 @@ void Value::CZString::swap(CZString &other) {
190190
std::swap(index_, other.index_);
191191
}
192192

193-
Value::CZString &Value::CZString::operator=(const CZString &other) {
194-
CZString temp(other);
195-
swap(temp);
193+
Value::CZString &Value::CZString::operator=(CZString other) {
194+
swap(other);
196195
return *this;
197196
}
198197

@@ -481,9 +480,8 @@ Value::~Value() {
481480
delete[] comments_;
482481
}
483482

484-
Value &Value::operator=(const Value &other) {
485-
Value temp(other);
486-
swap(temp);
483+
Value &Value::operator=(Value other) {
484+
swap(other);
487485
return *this;
488486
}
489487

0 commit comments

Comments
 (0)