C++ style guide: Difference between revisions

131 bytes added ,  24 August 2021
→‎C++ features: Overhaul text.
m (Add new recommendation on references)
(→‎C++ features: Overhaul text.)
Line 182: Line 182:
, consider using 'const' references.  This helps avoid overflowing the finite stack capacity
, consider using 'const' references.  This helps avoid overflowing the finite stack capacity
of a program while still ensuring that read-only access is enforced.
of a program while still ensuring that read-only access is enforced.
=== C++11 features ===
C++11 features are generally allowed. Check if the feature you want to
use has been already used.  If not, ask on the mailing list.


=== std::string ===
=== std::string ===


When an empty string is required, use "", rather than creating an empty
When an empty string is required, use <code>""</code>, rather than creating an empty
string object with std::string ().
string object with <code>std::string ()</code>.


=== auto ===
=== auto ===
Line 207: Line 201:
Always use one of the four C++ long style casting forms ({{codeline|static_cast}}, {{codeline|dynamic_cast}}, {{codeline|reinterpret_cast}}, {{codeline|const_cast}}) rather than C-style forms (type cast {{codeline|(new_type) variable}} or the function form {{codeline|new_type (variable)}}).
Always use one of the four C++ long style casting forms ({{codeline|static_cast}}, {{codeline|dynamic_cast}}, {{codeline|reinterpret_cast}}, {{codeline|const_cast}}) rather than C-style forms (type cast {{codeline|(new_type) variable}} or the function form {{codeline|new_type (variable)}}).


=== C++14 ===
=== C++11 features ===
 
A C++11 compatible compiler is required for [[Building | building Octave]].  Please make use of all C++11 features.
 
=== C++14, C++17, C++20 features ===


Do not use C++14 features.  Octave is widely used in very old systems and we
Try to avoid C++14, C++17, or C++20 features.  Octave is widely used in very old systems and we want them to be able to use up to date versions of Octave.  Building a recent compiler in such systems is not a trivial task so the limitation must happen
want them to be able to use up to date versions of Octave.  Building a recent
compiler in such systems is not a trivial task so the limitation must happen
in Octave.
in Octave.


An exception: code that requires C++14 feature must also implement an
If the implementation using a C++14, C++17, or C++20 feature is very beneficial.  Make it optional via <code>configure</code> feature detection or also implement an alternative code in the absence of said feature.  In any case, please get in contact with the Octave maintainers on [https://octave.discourse.group/c/maintainers/7 Discourse].
alternative code in the absence of said feature.  In such case, use a
configure check. This increases maintenance a lot, must be used sparsely,
and requires approval from other maintainers.


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">