C++ style guide: Difference between revisions

Jump to navigation Jump to search
925 bytes added ,  22 September 2017
Extend Doxygen style guide and prettify code examples using <syntaxhighlight>.
(Moved the doxygen style guide from Doxygen.)
(Extend Doxygen style guide and prettify code examples using <syntaxhighlight>.)
Line 22: Line 22:
some code formatting tools.  Example indenting:
some code formatting tools.  Example indenting:


<pre>
<syntaxhighlight lang="cpp">
if (have_args)
if (have_args)
   {
   {
Line 30: Line 30:
else
else
   idx.push_back (make_value_list (args, arg_nm, tmp));
   idx.push_back (make_value_list (args, arg_nm, tmp));
</pre>
</syntaxhighlight>


If you have nested {{codeline|if}} statements, use extra braces for extra
If you have nested {{codeline|if}} statements, use extra braces for extra
Line 40: Line 40:
innermost braces enclosing the split.  Example:
innermost braces enclosing the split.  Example:


<pre>
<syntaxhighlight lang="cpp">
SVD::type type = ((nargout == 0 || nargout == 1)
SVD::type type = ((nargout == 0 || nargout == 1)
                   ? SVD::sigma_only
                   ? SVD::sigma_only
                   : (nargin == 2) ? SVD::economy : SVD::std);
                   : (nargin == 2) ? SVD::economy : SVD::std);
</pre>
</syntaxhighlight>


=== Non indenting whitespace ===
=== Non indenting whitespace ===
Line 57: Line 57:
Declarations of pointers have the '*' character cuddled with the name of the variable.
Declarations of pointers have the '*' character cuddled with the name of the variable.


<pre>
<syntaxhighlight lang="cpp">
unsigned int *pointer_variable;
unsigned int *pointer_variable;
</pre>
</syntaxhighlight>


However, references have the '&' character cuddled with the type of the variable.
However, references have the '&' character cuddled with the type of the variable.


<pre>
<syntaxhighlight lang="cpp">
unsigned int& reference_variable;
unsigned int& reference_variable;
</pre>
</syntaxhighlight>


=== Line Length ===
=== Line Length ===
Line 77: Line 77:
Format function headers like this:
Format function headers like this:


<pre>
<syntaxhighlight lang="cpp">
static bool
static bool
matches_patterns (const string_vector& patterns, int pat_idx,
matches_patterns (const string_vector& patterns, int pat_idx,
                   int num_pat, const std::string& name)
                   int num_pat, const std::string& name)
</pre>
</syntaxhighlight>


The return type of the function and any modifiers are specified on the first
The return type of the function and any modifiers are specified on the first
Line 102: Line 102:
* Do not declare anything on the std namespace;
* Do not declare anything on the std namespace;


{{Code|namespace style on a .h file|<pre>
{{Code|namespace style on a .h file|<syntaxhighlight lang="cpp">
// Note indentation
// Note indentation
namespace octave
namespace octave
Line 115: Line 115:
   }
   }
}
}
</pre>}}
</syntaxhighlight>}}


{{Code|namespace style on a .cc file|<pre>
{{Code|namespace style on a .cc file|<syntaxhighlight lang="cpp">
// Note indentation and that functions are not defined
// Note indentation and that functions are not defined
// as "octave::math::foo:foo"
// as "octave::math::foo:foo"
Line 130: Line 130:
   }
   }
}
}
</pre>}}
</syntaxhighlight>}}


== Naming ==
== Naming ==
Line 205: Line 205:
and requires approval from other maintainers.
and requires approval from other maintainers.


#if defined (HAVE_THIS_C14_FEATURE)
<syntaxhighlight lang="cpp">
  // code that really needs it
#if defined (HAVE_THIS_C14_FEATURE)
#else
  // code that really needs it
  // alternative code in its absence
#else
#endif
  // alternative code in its absence
#endif
</syntaxhighlight>


== Doxygen ==
== Doxygen ==
Line 220: Line 222:


* For Doxygen comments use only {{codeline|//!}} and NOT {{codeline|/*! ... */}}
* For Doxygen comments use only {{codeline|//!}} and NOT {{codeline|/*! ... */}}
* Use {{codeline|@}} for any Doxygen Special Commands
* Use {{codeline|@}} for any [https://www.stack.nl/~dimitri/doxygen/manual/commands.html Doxygen Special Commands]
* Use as little formatting as possible.  Restrict to [https://www.stack.nl/~dimitri/doxygen/manual/markdown.html Markdown] and avoid HTML-markup.
* Leave a blank line between the Doxygen comment and function definition.


An example of properly used Doxygen would look like:
An example of properly used Doxygen would look like:
 
<pre>
//! Comment with special command and syntax highlighting:


<syntaxhighlight lang="cpp">
//! @brief Some cool function.
//!
//! Long comment with **bold** special commands.
//!
//! @param some_param Really should figure out what to do.
//! @param another_param Does something cool with @p some_param.
//!
//! And some example using syntax highlighting:
//!
//! @code{.cc}
//! @code{.cc}
//! double v = 1.0;
//! double v = 1.0;
//! int n = 2;
//! some_function (v, n);
//! @endcode
//! @endcode
</pre>
 
void some_function (double some_param, int another_param) {
  // ...
}
</syntaxhighlight>


== Comments ==
== Comments ==

Navigation menu