GNU's C ++ code writing specification, C language father Dennis Ritchie personally revised

xiaoxiao2021-03-06  39

C Standard Library Style Guidelines Draft 1999-02-26 ----------------------------------- This Library is written to appropriate C coding standards As such, it is intended to precede the recommendations of the GNU CodingStandard, which can be referenced here:. http: //www.gnu.ai.mit.edu/prep/standards_toc.html ChangeLog entries for member functions should use theclassname :: member function name syntax as follows: 1999-04-15 Dennis Ritchie * src / basic_file.cc (__basic_file :: open): Fix thinko in_G_HAVE_IO_FILE_OPEN bits.Notable areas of Divergence from What May Be Previous Local Practice (Particular For GNU C) include: 01. Pointers and References Char * P = "flop"; char & c = * p; -not- char * p = "flop"; // wrong char & c = * p; // Wrong Reason: IN C , Definitions Are Mixed with Executable Code. Here, P Is Being Initialized, Not * P. this is Near-Universal Practice Among C Programmers; IT IS Normal for c Hackets To switch spontaneously as they gain experience.02 Operator names and parentheses operator == (type) -NOT- operator == (type) // wrong Reason:. The == is part of the function name Separating it makes the declaration look like. An expression. 03. function nameless: noid mangle () -not- void mangle () // WRONG REASON: No Space Before Parenthesees (Except After a control-flow keyword) Is Near-Universal Practice for C

. It identifies the parentheses as the function-call operator or declarator, as opposed to an expression or other overloaded use of parentheses.04. Template function indentation template void template_function (args) {} -NOT- template void template_function (args) {}; Reason: In class definitions, without indentation whitespace is needed both above and below the declaration to distinguish it visually from other members (Also, re: "typename" rather than "class".) T. often could be int, which is not a class ( "class", here, is an anachronism.) 05 Template class indentation template class basic_ios: public ios_base {public: // Types:}..; -Not- template Class Basic_ios: public ios_base {public: // types:}; -not- template class Basic_ios: public ios_base {public: / / Types:}; 06 Enumerators enum {space = _ISspace, print = _ISprint, cntrl = _IScntrl,}; -NOT- enum {space = _ISspace, print = _ISprint, cntrl = _IScntrl}; 07 Member initialization lists All one line.. Separate from class name. gribble :: gribble (): _m_private_data (0), _m_more_stuff (0), _m_helper (0); {} -not- gribble :: gribble (): _m_private_data (0), _m_more_stuff (0), _M_HELPER (0);

{} 08. Try / Catch Blocks Try {//} catch (...) {//} - NOT- Try {//} catch (...) {//} 09. Member Functions Declarations and Defintions Keywords Such . as extern, static, export, explicit, inline, etc go on the line above the function name Thus virtual int foo () -NOT- virtual int foo () Reason: GNU coding conventions dictate return types for functions are on a separate line than the function name and parameter list for definitions. for C , where we have member functions that can. be either inline definitions or declarations, keeping to this standard allows all member function names for a given class to bealigned to the same margin, increasing readibility .10. Invocation of Member Functions with "this->" for non-uglified names, user this-> name to call the function. This-> sync () -not- sync () THE LIBRARY CURRENTLY HAS A MIXTURE OF GNU- C and Modern C CodingStyles. The GNU C Usages Will Be Combed Out Gradually.Name Patterns: for Nonstandard Name s appearing in Standard headers, we are constrained to use names that begin with underscores This is called "uglification" .The convention is:. Local and argument names: __ [az] * Examples:. __count __ix __s1 Type names and template formal- argument names: _ [AZ] [^ _] * Examples:. _Helper _CharT _N Member data and function names: _M _ * Examples:. _M_num_elements _M_initialize () Static data members, constants, and enumerations:. _S _ * Examples: _S_max_elements _S_default_valueDon ' THAT DIFFER ON THE SAME Scope That Difer Only in The Prefix, Eg_Top and _m_top. See Badnames for a list of forbidden name. (The Most Tempting of these Seem to be and "_t" and "

__sz ".) Names Must Never Have" __ "ITENALLY; It Would Confuse Nameunmanglers on Some Targets. Also, Never USE" __ [0-9] ", Same Reason. ------------- ------------- [by example] #ifndef _header_ # define _header_ 1namespace std {class gribble {public: // ctor, op =, dtor gribble () throw (); Gribble (Const Gribble & ); explicit gribble (int __howmany); gribble & operator = (const gribble &); virtual ~ gribble () throw (); // argument inline void public_member (const char * __arg) const; // in-class function definitions should be restricted To one-line (incount) {Return Strchr (arg, 'a');} inline int three_lines (); // inline, but defined Below. // Note Indentation Template void other_template (); private: class_helper; int _m_private_data; int _m_private_data; int _M_more_stuff; _Helper * _M_helper; int _M_private_function (); enum _Enum {_S_one, _S_two}; static void _S_initialize_library ();}; // More-or-less-standard language features described by lack, not presence: # ifndef _G_NO_LONGLONG extern long ! long _G_global_with_a_good_long_name; // avoid globals # endif // avoid in-class inline definitions, define separately; // likewise for member class definitions: inline int gribble :: public_member () const {int __local = 0; return __local;} class Gribble :: _ helper {int _m_stuff; Friend Class Gribble;};} // Names Beginning with "__": ONLY for arguments and // local variables

转载请注明原文地址:https://www.9cbs.com/read-78371.html

New Post(0)