Preferred style of casting?

I looked at the coding guidelines for C++ on the wiki but I didn’t see this topic addressed.

I know there is a preference not to use C-style explicit casting such as (double) int_variable. Is it okay to use C-style functional casting such as double (int_variable)?

Or, is the preference for the very long form C++ style static_cast<double> (int_variable)?

The current decision seems to prefer static_cast<double> (), as -Wold-style-cast is active by default, see https://hg.savannah.gnu.org/hgweb/octave/file/54509b7fddd3/configure.ac#l2684.

Personally, I like the longer notation too, as it documents what the programmer expects to happen (static_cast, const_cast, reinterpret_cast).

IIUC, the C-style casting and functional casting are basically equivalent (with the same difficult to deduce rules what actually happens with the cast argument).
I’d personally prefer the long C++ style cast. Even if that is quite verbose. But I believe that verbosity is necessary to make the code clearer and possibly avoid coding errors.

Using the longer C++ style cast syntax also makes it easier to find (and maybe eliminate) them. If possible, I would like to avoid casting unless it is really necessary so being able to locate them is helpful to me.

1 Like

Do we want to maintain the coding conventions on the wiki? The motivation for this thread was that this design choice was undocumented, but it seems like there is a preference for the long form.

Documenting coding conventions would be good. A wiki page also seems like a good place to start. Do we have one already?

Yes, the guidelines for contributing patches, along with style guides for C/C++, m-files, and Fortran were removed from the Octave manual and now live on the Octave Wiki only. C++ style guide is at https://wiki.octave.org/C%2B%2B_style_guide. I made the change.