1,072
edits
m (→references) |
(Add section for lambda expressions) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 84: | Line 84: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== #ifdef directives ==== | |||
Indent code that follows a conditional processor directive such as {{codeline|#ifdef}} or {{codeline|#else}}. | |||
Example | |||
<syntaxhighlight lang="cpp"> | |||
#if defined (HAVE_CONFIG_H) | |||
# include "config.h" | |||
#endif | |||
</syntaxhighlight> | |||
The '#' character may also be placed with the directive rather than remaining in column 1 if this looks better. | |||
==== Split long expressions ==== | ==== Split long expressions ==== | ||
Line 155: | Line 169: | ||
=== Namespace === | === Namespace === | ||
All code should be in the octave namespace. | All code should be in the {{codeline|octave}} namespace or in a namespace below it. | ||
should | Namespaces should start and stop using the special macros {{codeline|OCTAVE_BEGIN_NAMESPACE(XXX)}} and {{codeline|OCTAVE_END_NAMESPACE(XXX)}}. There is no indentation of code that is placed into namespaces using these macros. | ||
Example | |||
{{Code|Use of namespace macros|<syntaxhighlight lang="cpp"> | |||
OCTAVE_BEGIN_NAMESPACE(octave) | |||
OCTAVE_BEGIN_NAMESPACE(math) | |||
template <typename T> | |||
void | |||
umfpack_report_control (const double *Control); | |||
OCTAVE_END_NAMESPACE(math) | |||
OCTAVE_END_NAMESPACE(octave) | |||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
{{Code|namespace | If bare namespace directives must be used, as occasionally is required in Qt code, then the code within the namespace should be indented. | ||
// Note indentation and that functions are not defined | |||
{{Code|bare namespace usage|<syntaxhighlight lang="cpp"> | |||
// Note indentation and that functions are not defined as "octave::math::foo:foo" | |||
namespace octave | namespace octave | ||
{ | { | ||
Line 195: | Line 203: | ||
} | } | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
==== Other Guidelines ==== | |||
* Do not use {{codeline|using XXX;}} directives | |||
* Do not declare anything on the {{codeline|std::}} namespace | |||
== Naming == | == Naming == | ||
Line 315: | Line 327: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|} | |} | ||
=== new/delete === | |||
Pointers that will be allocated memory with {{codeline|new}} should be initialized with the C++ literal {{codeline|nullptr}}, not the numerical value 0 or the macro {{codeline|NULL}}. | |||
The {{codeline|delete}} keyword accepts {{codeline|nullptr}} and programmers should not put an {{codeline|if (ptr)}} guard around {{codeline|delete}}. | |||
{| class="wikitable" | |||
! style="color:green;" | good | |||
! style="color:darkred;" | bad | |||
|- | |||
| <syntaxhighlight lang="c++"> | |||
delete ptr; | |||
</syntaxhighlight> | |||
| <syntaxhighlight lang="c++"> | |||
if (ptr) | |||
delete ptr; | |||
</syntaxhighlight> | |||
|} | |||
=== lambda expressions === | |||
When capturing variables from the surrounding function, explicitly list the variables being captured rather than relying on a default capture by value (`[=]`) or by reference (`[&]`). This more clearly captures the programmer's intent and makes the code more understandable. | |||
=== std::string === | === std::string === |
edits