Editing C++ style guide
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 7: | Line 7: | ||
== Formatting == | == Formatting == | ||
=== Indentation === | === Indentation === | ||
Use only spaces, and indent 2 spaces at a time. | |||
We use spaces for indentation. Absolutely do not use tabs in your code. | |||
You should probably set your editor to emit spaces when you hit the tab key. | |||
=== Line Length === | |||
Keep the length of source lines to 79 characters or less, for maximum | |||
readability in the widest range of environments. This is inherited from | |||
the [https://www.gnu.org/prep/standards/standards.html#Formatting GNU Coding Standards]. | |||
=== Namespace === | |||
All code should be in the octave namespace. This is an ongoing project. We | |||
are still moving existing classes into namespaces but all new classes | |||
should go somewhere into the "octave" namespace. There is 1 extra level for namespaces | |||
inside octave to be used with care, we don't want too many namespaces. | |||
Ask before creating a new namespace. | |||
* Indent namespaces as any other block. Emacs and other editors can do this automatically. | |||
* Define namespace on the .cc files; | |||
* Do not use "using X" directives; | |||
* Do not declare anything on the std namespace; | |||
< | {{Code|namespace style on a .h file|<pre> | ||
// Note indentation | |||
namespace octave | |||
{ | { | ||
namespace math | |||
{ | { | ||
class foo | |||
{ | { | ||
public: | |||
foo (...); | |||
} | }; | ||
} | } | ||
} | |||
</pre>}} | |||
</ | |||
{{Code| | {{Code|namespace style on a .cc file|<pre> | ||
// Note indentation and that functions are not defined as "octave::math::foo:foo" | // Note indentation and that functions are not defined | ||
// as "octave::math::foo:foo" | |||
namespace octave | namespace octave | ||
{ | { | ||
Line 202: | Line 62: | ||
} | } | ||
} | } | ||
</ | </pre>}} | ||
== Header Files == | == Header Files == | ||
Line 258: | Line 98: | ||
Header files should not use any "#if defined (HAVE_FEATURE)" conditionals. This is not quite true yet, but we are almost there. '''No new conditionals may be added.''' | Header files should not use any "#if defined (HAVE_FEATURE)" conditionals. This is not quite true yet, but we are almost there. '''No new conditionals may be added.''' | ||
== C++ features == | == Other C++ features == | ||
=== | === C++11 === | ||
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. | |||
==== auto ==== | |||
Use of {{codeline|auto}} is allowed only where it helps readability | Use of {{codeline|auto}} is allowed only where it helps readability | ||
Line 365: | Line 114: | ||
* Beware of copy when using {{codeline|auto}} in for loops. Pass by reference and use {{codeline|const}} unless you're dealing with simple types such as {{codeline|int}}. See [http://lists.gnu.org/archive/html/octave-maintainers/2016-06/msg00144.html 'auto' uses and for-range loops] on the maintainers mailing list for more details. | * Beware of copy when using {{codeline|auto}} in for loops. Pass by reference and use {{codeline|const}} unless you're dealing with simple types such as {{codeline|int}}. See [http://lists.gnu.org/archive/html/octave-maintainers/2016-06/msg00144.html 'auto' uses and for-range loops] on the maintainers mailing list for more details. | ||
=== C++14 === | |||
=== C++14 | |||
Do not use C++14 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 | |||
in Octave. | in Octave. | ||
An exception: code that requires C++14 feature must also implement an | |||
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. | |||
#if defined (HAVE_THIS_C14_FEATURE) | |||
// code that really needs it | |||
#else | |||
// alternative code in its absence | |||
#endif |