PERF: Allow compiler to choose best way to construct a copy

With move semantics added to the language and the standard library updated with
move constructors added for many types it is now interesting to take an
argument directly by value, instead of by const-reference, and then copy. This
check allows the compiler to take care of choosing the best way to construct
the copy.

The transformation is usually beneficial when the calling code passes an rvalue
and assumes the move construction is a cheap operation. This short example
illustrates how the construction of the value happens:

SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC
BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD

cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,modernize-pass-by-value  -header-filter=.* -fix
This commit is contained in:
Hans Johnson 2019-01-14 17:09:19 -06:00 committed by Hans Johnson
parent 1fc3de7ca1
commit b5093e8122
5 changed files with 19 additions and 20 deletions

View File

@ -54,7 +54,7 @@ namespace Json {
*/
class JSON_API Exception : public std::exception {
public:
Exception(JSONCPP_STRING const& msg);
Exception(JSONCPP_STRING msg);
~Exception() JSONCPP_NOEXCEPT override;
char const* what() const JSONCPP_NOEXCEPT override;

View File

@ -300,8 +300,8 @@ public:
/**
* \param indentation Each level will be indented by this amount extra.
*/
StyledStreamWriter(const JSONCPP_STRING& indentation = "\t");
~StyledStreamWriter() {}
StyledStreamWriter(JSONCPP_STRING indentation = "\t");
~StyledStreamWriter() = default;
public:
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.

View File

@ -88,9 +88,8 @@ bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
// //////////////////////////////////////////////////////////////////
Reader::Reader()
: errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
lastValue_(), commentsBefore_(), features_(Features::all()),
collectComments_() {}
: errors_(), document_(), commentsBefore_(), features_(Features::all())
{}
Reader::Reader(const Features& features)
: errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),

View File

@ -213,7 +213,7 @@ static inline void releaseStringValue(char* value, unsigned) { free(value); }
namespace Json {
Exception::Exception(JSONCPP_STRING const& msg) : msg_(msg) {}
Exception::Exception(JSONCPP_STRING msg) : msg_(std::move(msg)) {}
Exception::~Exception() JSONCPP_NOEXCEPT {}
char const* Exception::what() const JSONCPP_NOEXCEPT { return msg_.c_str(); }
RuntimeError::RuntimeError(JSONCPP_STRING const& msg) : Exception(msg) {}

View File

@ -642,8 +642,8 @@ bool StyledWriter::hasCommentForValue(const Value& value) {
// Class StyledStreamWriter
// //////////////////////////////////////////////////////////////////
StyledStreamWriter::StyledStreamWriter(const JSONCPP_STRING& indentation)
: document_(nullptr), rightMargin_(74), indentation_(indentation),
StyledStreamWriter::StyledStreamWriter(JSONCPP_STRING indentation)
: document_(nullptr), indentation_(std::move(indentation)),
addChildValues_(), indented_(false) {}
void StyledStreamWriter::write(JSONCPP_OSTREAM& out, const Value& root) {
@ -872,11 +872,11 @@ struct CommentStyle {
};
struct BuiltStyledStreamWriter : public StreamWriter {
BuiltStyledStreamWriter(JSONCPP_STRING const& indentation,
BuiltStyledStreamWriter(JSONCPP_STRING indentation,
CommentStyle::Enum cs,
JSONCPP_STRING const& colonSymbol,
JSONCPP_STRING const& nullSymbol,
JSONCPP_STRING const& endingLineFeedSymbol,
JSONCPP_STRING colonSymbol,
JSONCPP_STRING nullSymbol,
JSONCPP_STRING endingLineFeedSymbol,
bool useSpecialFloats,
unsigned int precision,
PrecisionType precisionType);
@ -912,17 +912,17 @@ private:
PrecisionType precisionType_;
};
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
JSONCPP_STRING const& indentation,
JSONCPP_STRING indentation,
CommentStyle::Enum cs,
JSONCPP_STRING const& colonSymbol,
JSONCPP_STRING const& nullSymbol,
JSONCPP_STRING const& endingLineFeedSymbol,
JSONCPP_STRING colonSymbol,
JSONCPP_STRING nullSymbol,
JSONCPP_STRING endingLineFeedSymbol,
bool useSpecialFloats,
unsigned int precision,
PrecisionType precisionType)
: rightMargin_(74), indentation_(indentation), cs_(cs),
colonSymbol_(colonSymbol), nullSymbol_(nullSymbol),
endingLineFeedSymbol_(endingLineFeedSymbol), addChildValues_(false),
: rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
endingLineFeedSymbol_(std::move(endingLineFeedSymbol)), addChildValues_(false),
indented_(false), useSpecialFloats_(useSpecialFloats),
precision_(precision), precisionType_(precisionType) {}
int BuiltStyledStreamWriter::write(Value const& root, JSONCPP_OSTREAM* sout) {