osrf / gazebo_design

Design documents for the Gazebo project.
http://gazebosim.org/
0 stars 1 forks source link

cppcheck errors: constructor with 1 argument that is not explicit #2

Open osrf-migration opened 8 years ago

osrf-migration commented 8 years ago

Original report (archived issue) by Steve Peters (Bitbucket: Steven Peters, GitHub: scpeters).


Several of our packages have cppcheck errors on recent OS versions, such as Ubuntu Wily that say Class 'X' has a constructor with 1 argument that is not explicit.

For example, it complains about the Matrix3(const Quaternion &_q) constructor.

What is the right thing to do about this?

osrf-migration commented 8 years ago

Original comment by Jose Luis Rivero (Bitbucket: Jose Luis Rivero, GitHub: j-rivero).


I wouldn't say that there is 'a right thing to do'. Depending on what we are looking for when defining the constructor we can choose one way or the other.

I think that what we are discussing here is what c++ called a "converting constructor" I found it well explained in the cppreference page.

About the pros vs cons, this programmers Q&A answers can help.

osrf-migration commented 8 years ago

Original comment by Jose Luis Rivero (Bitbucket: Jose Luis Rivero, GitHub: j-rivero).


As a rule of thumb: declare any single parameter constructor explicit unless you have a good reason to allow implicit conversions.

Note the explicit keyword works for: avoid implicit conversions and implicit copy-initialization (A a = v, which is different of direct initialization A a(v)). And implicit conversions follow not a simple logic.