2 C++ Standard Library Style Guidelines DRAFT 2001-01-15
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 here:
9 http://www.gnu.org/prep/standards_toc.html
11 ChangeLog entries for member functions should use the
12 classname::member function name syntax as follows:
14 1999-04-15 Dennis Ritchie <dr@att.com>
16 * src/basic_file.cc (__basic_file::open): Fix thinko in
17 _G_HAVE_IO_FILE_OPEN bits.
19 Notable areas of divergence from what may be previous local practice
20 (particularly for GNU C) include:
22 01. Pointers and references
26 char *p = "flop"; // wrong
27 char &c = *p; // wrong
29 Reason: In C++, definitions are mixed with executable code. Here,
30 p is being initialized, not *p. This is near-universal
31 practice among C++ programmers; it is normal for C hackers
32 to switch spontaneously as they gain experience.
34 02. Operator names and parentheses
37 operator == (type) // wrong
39 Reason: The == is part of the function name. Separating
40 it makes the declaration look like an expression.
42 03. Function names and parentheses
45 void mangle () // wrong
47 Reason: no space before parentheses (except after a control-flow
48 keyword) is near-universal practice for C++. It identifies the
49 parentheses as the function-call operator or declarator, as
50 opposed to an expression or other overloaded use of parentheses.
52 04. Template function indentation
55 template_function(args)
59 void template_function(args) {};
61 Reason: In class definitions, without indentation whitespace is
62 needed both above and below the declaration to distinguish
63 it visually from other members. (Also, re: "typename"
64 rather than "class".) T often could be int, which is
65 not a class. ("class", here, is an anachronism.)
67 05. Template class indentation
68 template<typename _CharT, typename _Traits>
69 class basic_ios : public ios_base
75 template<class _CharT, class _Traits>
76 class basic_ios : public ios_base
82 template<class _CharT, class _Traits>
83 class basic_ios : public ios_base
97 enum { space = _ISspace, print = _ISprint, cntrl = _IScntrl };
99 07. Member initialization lists
100 All one line, separate from class name.
103 : _M_private_data(0), _M_more_stuff(0), _M_helper(0);
106 gribble::gribble() : _M_private_data(0), _M_more_stuff(0), _M_helper(0);
125 09. Member functions declarations and defintions
126 Keywords such as extern, static, export, explicit, inline, etc
127 go on the line above the function name. Thus
134 Reason: GNU coding conventions dictate return types for functions
135 are on a separate line than the function name and parameter list
136 for definitions. For C++, where we have member functions that can
137 be either inline definitions or declarations, keeping to this
138 standard allows all member function names for a given class to be
139 aligned to the same margin, increasing readibility.
142 10. Invocation of member functions with "this->"
143 For non-uglified names, use this->name to call the function.
149 Reason: Koenig lookup.
163 12. Spacing under protected and private in class declarations:
164 space above, none below
175 13. Spacing WRT return statements.
176 no extra spacing before returns
187 14. Location of global variables.
188 All global variables of class type, whether in the "user visable"
189 space (e.g., cin) or the implementation namespace, must be defined
190 as a character array with the appropriate alignment and then later
191 re-initialized to the correct value.
193 This is due to startup issues on certain platforms, such as AIX.
194 For more explanation and examples, see src/globals.cc. All such
195 variables should be contained in that file, for simplicity.
199 The library currently has a mixture of GNU-C and modern C++ coding
200 styles. The GNU C usages will be combed out gradually.
204 For nonstandard names appearing in Standard headers, we are constrained
205 to use names that begin with underscores. This is called "uglification".
208 Local and argument names: __[a-z].*
210 Examples: __count __ix __s1
212 Type names and template formal-argument names: _[A-Z][^_].*
214 Examples: _Helper _CharT _N
216 Member data and function names: _M_.*
218 Examples: _M_num_elements _M_initialize ()
220 Static data members, constants, and enumerations: _S_.*
222 Examples: _S_max_elements _S_default_value
224 Don't use names in the same scope that differ only in the prefix,
225 e.g. _S_top and _M_top. See BADNAMES for a list of forbidden names.
226 (The most tempting of these seem to be and "_T" and "__sz".)
228 Names must never have "__" internally; it would confuse name
229 unmanglers on some targets. Also, never use "__[0-9]", same reason.
231 --------------------------
246 gribble(const gribble&);
249 gribble(int __howmany);
252 operator=(const gribble&);
259 public_member(const char* __arg) const;
261 // in-class function definitions should be restricted to one-liners.
263 one_line() { return 0 }
266 two_lines(const char* arg)
267 { return strchr(arg, 'a'); }
270 three_lines(); // inline, but defined below.
273 template<typename _Formal_argument>
275 public_template() const throw();
277 template<typename _Iterator>
287 int _M_private_function();
296 _S_initialize_library();
299 // More-or-less-standard language features described by lack, not presence:
300 # ifndef _G_NO_LONGLONG
301 extern long long _G_global_with_a_good_long_name; // avoid globals!
304 // Avoid in-class inline definitions, define separately;
305 // likewise for member class definitions:
307 gribble::public_member() const
308 { int __local = 0; return __local; }
310 class gribble::_Helper
314 friend class gribble;
318 // Names beginning with "__": only for arguments and
319 // local variables; never use "__" in a type name, or
320 // within any name; never use "__[0-9]".
322 #endif /* _HEADER_ */
327 template<typename T> // notice: "typename", not "class", no space
328 long_return_value_type<with_many, args>
329 function_name(char* pointer, // "char *pointer" is wrong.
331 const Reference& ref)
333 // int a_local; /* wrong; see below. */
339 int a_local = 0; // declare variable at first use.
341 // char a, b, *p; /* wrong */
344 char* c = "abc"; // each variable goes on its own line, always.
346 // except maybe here...
347 for (unsigned i = 0, mask = 1; mask; ++i, mask <<= 1) {
353 : _M_private_data(0), _M_more_stuff(0), _M_helper(0);
357 gribble::three_lines()
359 // doesn't fit in one line.