2 C++ Standard Library Coding Style Guidelines
3 -------------------------------------
5 This library is written to appropriate C++ coding standards. As such,
6 it is intended to precede the recommendations of the GNU Coding
7 Standard, which can be referenced in full here:
9 http://www.gnu.org/prep/standards/standards.html#Formatting
11 The rest of this is also interesting reading, but skip the "Design
14 The GCC coding conventions are here, and are also useful:
15 http://gcc.gnu.org/codingconventions.html
17 In addition, because it doesn't seem to be stated explicitly anywhere
18 else, there is an 80 column source limit.
20 ChangeLog entries for member functions should use the
21 classname::member function name syntax as follows:
23 1999-04-15 Dennis Ritchie <dr@att.com>
25 * src/basic_file.cc (__basic_file::open): Fix thinko in
26 _G_HAVE_IO_FILE_OPEN bits.
28 Notable areas of divergence from what may be previous local practice
29 (particularly for GNU C) include:
31 01. Pointers and references
35 char *p = "flop"; // wrong
36 char &c = *p; // wrong
38 Reason: In C++, definitions are mixed with executable code. Here,
39 p is being initialized, not *p. This is near-universal
40 practice among C++ programmers; it is normal for C hackers
41 to switch spontaneously as they gain experience.
43 02. Operator names and parentheses
46 operator == (type) // wrong
48 Reason: The == is part of the function name. Separating
49 it makes the declaration look like an expression.
51 03. Function names and parentheses
54 void mangle () // wrong
56 Reason: no space before parentheses (except after a control-flow
57 keyword) is near-universal practice for C++. It identifies the
58 parentheses as the function-call operator or declarator, as
59 opposed to an expression or other overloaded use of parentheses.
61 04. Template function indentation
64 template_function(args)
68 void template_function(args) {};
70 Reason: In class definitions, without indentation whitespace is
71 needed both above and below the declaration to distinguish
72 it visually from other members. (Also, re: "typename"
73 rather than "class".) T often could be int, which is
74 not a class. ("class", here, is an anachronism.)
76 05. Template class indentation
77 template<typename _CharT, typename _Traits>
78 class basic_ios : public ios_base
84 template<class _CharT, class _Traits>
85 class basic_ios : public ios_base
91 template<class _CharT, class _Traits>
92 class basic_ios : public ios_base
106 enum { space = _ISspace, print = _ISprint, cntrl = _IScntrl };
108 07. Member initialization lists
109 All one line, separate from class name.
112 : _M_private_data(0), _M_more_stuff(0), _M_helper(0);
115 gribble::gribble() : _M_private_data(0), _M_more_stuff(0), _M_helper(0);
134 09. Member functions declarations and definitions
135 Keywords such as extern, static, export, explicit, inline, etc
136 go on the line above the function name. Thus
143 Reason: GNU coding conventions dictate return types for functions
144 are on a separate line than the function name and parameter list
145 for definitions. For C++, where we have member functions that can
146 be either inline definitions or declarations, keeping to this
147 standard allows all member function names for a given class to be
148 aligned to the same margin, increasing readibility.
151 10. Invocation of member functions with "this->"
152 For non-uglified names, use this->name to call the function.
158 Reason: Koenig lookup.
172 12. Spacing under protected and private in class declarations:
173 space above, none below
184 13. Spacing WRT return statements.
185 no extra spacing before returns, no parenthesis
202 14. Location of global variables.
203 All global variables of class type, whether in the "user visable"
204 space (e.g., cin) or the implementation namespace, must be defined
205 as a character array with the appropriate alignment and then later
206 re-initialized to the correct value.
208 This is due to startup issues on certain platforms, such as AIX.
209 For more explanation and examples, see src/globals.cc. All such
210 variables should be contained in that file, for simplicity.
212 15. Exception abstractions
213 Use the exception abstractions found in functexcept.h, which allow
214 C++ programmers to use this library with -fno-exceptions. (Even if
215 that is rarely advisable, it's a necessary evil for backwards
218 16. Exception error messages
219 All start with the name of the function where the exception is
220 thrown, and then (optional) descriptive text is added. Example:
222 __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));
224 Reason: The verbose terminate handler prints out exception::what(),
225 as well as the typeinfo for the thrown exception. As this is the
226 default terminate handler, by putting location info into the
227 exception string, a very useful error message is printed out for
228 uncaught exceptions. So useful, in fact, that non-programmers can
229 give useful error messages, and programmers can intelligently
230 speculate what went wrong without even using a debugger.
232 17. The doxygen style guide to comments is a separate document,
235 The library currently has a mixture of GNU-C and modern C++ coding
236 styles. The GNU C usages will be combed out gradually.
240 For nonstandard names appearing in Standard headers, we are constrained
241 to use names that begin with underscores. This is called "uglification".
244 Local and argument names: __[a-z].*
246 Examples: __count __ix __s1
248 Type names and template formal-argument names: _[A-Z][^_].*
250 Examples: _Helper _CharT _N
252 Member data and function names: _M_.*
254 Examples: _M_num_elements _M_initialize ()
256 Static data members, constants, and enumerations: _S_.*
258 Examples: _S_max_elements _S_default_value
260 Don't use names in the same scope that differ only in the prefix,
261 e.g. _S_top and _M_top. See BADNAMES for a list of forbidden names.
262 (The most tempting of these seem to be and "_T" and "__sz".)
264 Names must never have "__" internally; it would confuse name
265 unmanglers on some targets. Also, never use "__[0-9]", same reason.
267 --------------------------
281 gribble(const gribble&);
284 gribble(int __howmany);
287 operator=(const gribble&);
292 // Start with a capital letter, end with a period.
294 public_member(const char* __arg) const;
296 // In-class function definitions should be restricted to one-liners.
298 one_line() { return 0 }
301 two_lines(const char* arg)
302 { return strchr(arg, 'a'); }
305 three_lines(); // inline, but defined below.
308 template<typename _Formal_argument>
310 public_template() const throw();
312 template<typename _Iterator>
322 int _M_private_function();
331 _S_initialize_library();
334 // More-or-less-standard language features described by lack, not presence.
335 # ifndef _G_NO_LONGLONG
336 extern long long _G_global_with_a_good_long_name; // avoid globals!
339 // Avoid in-class inline definitions, define separately;
340 // likewise for member class definitions:
342 gribble::public_member() const
343 { int __local = 0; return __local; }
345 class gribble::_Helper
349 friend class gribble;
353 // Names beginning with "__": only for arguments and
354 // local variables; never use "__" in a type name, or
355 // within any name; never use "__[0-9]".
357 #endif /* _HEADER_ */
362 template<typename T> // notice: "typename", not "class", no space
363 long_return_value_type<with_many, args>
364 function_name(char* pointer, // "char *pointer" is wrong.
366 const Reference& ref)
368 // int a_local; /* wrong; see below. */
374 int a_local = 0; // declare variable at first use.
376 // char a, b, *p; /* wrong */
379 char* c = "abc"; // each variable goes on its own line, always.
381 // except maybe here...
382 for (unsigned i = 0, mask = 1; mask; ++i, mask <<= 1) {
388 : _M_private_data(0), _M_more_stuff(0), _M_helper(0);
392 gribble::three_lines()
394 // doesn't fit in one line.