Skip to content

Commit 00cd55d

Browse files
committed
Implement ParsedFunction copy ctor/assignment operator using copy-and-swap idiom
1 parent a6f14fe commit 00cd55d

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

include/numerics/parsed_function.h

+14-13
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,18 @@ ParsedFunction<Output,OutputGradient>::ParsedFunction (const std::string & expre
214214
template <typename Output, typename OutputGradient>
215215
inline
216216
ParsedFunction<Output,OutputGradient>::ParsedFunction (const ParsedFunction<Output,OutputGradient> & other) :
217-
FunctionBase<Output>()
217+
FunctionBase<Output>(other),
218+
_expression(other._expression),
219+
_subexpressions(other._subexpressions),
220+
_spacetime(other._spacetime),
221+
_valid_derivatives(other._valid_derivatives),
222+
variables(other.variables),
223+
_additional_vars(other._additional_vars),
224+
_initial_vals(other._initial_vals)
218225
{
219-
*this = other;
226+
// parsers can be generated from scratch by reparsing expression
227+
this->reparse(this->_expression);
228+
this->_initialized = true;
220229
}
221230

222231

@@ -226,17 +235,9 @@ inline
226235
ParsedFunction<Output,OutputGradient> &
227236
ParsedFunction<Output,OutputGradient>::operator= (const ParsedFunction<Output,OutputGradient> & other)
228237
{
229-
this->_master = other._master;
230-
this->_expression = other._expression;
231-
this->_spacetime = other._spacetime;
232-
this->_valid_derivatives = other._valid_derivatives;
233-
this->_additional_vars = other._additional_vars;
234-
this->_initial_vals = other._initial_vals;
235-
236-
// parsers can be generated from scratch by reparsing expression
237-
this->reparse(this->_expression);
238-
this->_initialized = true;
239-
238+
// Use copy-and-swap idiom
239+
ParsedFunction<Output,OutputGradient> tmp(other);
240+
std::swap(tmp, *this);
240241
return *this;
241242
}
242243

0 commit comments

Comments
 (0)