分享
 
 
 

GNU的C++代码书写规范,C语言之父Dennis Ritchie亲自修订

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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 Coding

Standard, which can be referenced here:

http://www.gnu.ai.mit.edu/prep/standards_toc.html

ChangeLog entries for member functions should use the

classname::member function name syntax as follows:

1999-04-15 Dennis Ritchie <dr@att.com>

* 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

(particularly 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 hackers

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 names and parentheses

void mangle()

-NOT-

void mangle () // wrong

Reason: no space before parentheses (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<typename T>

void

template_function(args)

{ }

-NOT-

template<class T>

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<typename _CharT, typename _Traits>

class basic_ios : public ios_base

{

public:

// Types:

};

-NOT-

template<class _CharT, class _Traits>

class basic_ios : public ios_base

{

public:

// Types:

};

-NOT-

template<class _CharT, class _Traits>

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 be

aligned to the same margin, increasing readibility.

10. Invocation of member functions with "this->"

For non-uglified names, use this->name to call the function.

this->sync()

-NOT-

sync()

The library currently has a mixture of GNU-C and modern C++ coding

styles. The GNU C usages will be combed out gradually.

Name patterns:

For nonstandard names 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: __[a-z].*

Examples: __count __ix __s1

Type names and template formal-argument names: _[A-Z][^_].*

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_value

Don't use names in the same scope that differ only in the prefix,

e.g. _S_top and _M_top. See BADNAMES for a list of forbidden names.

(The most tempting of these seem to be and "_T" and "__sz".)

Names must never have "__" internally; it would confuse name

unmanglers on some targets. Also, never use "__[0-9]", same reason.

--------------------------

[BY EXAMPLE]

#ifndef _HEADER_

#define _HEADER_ 1

namespace 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-liners.

int

one_line() { return 0 }

int

two_lines(const char* arg)

{ return strchr(arg, 'a'); }

inline int

three_lines(); // inline, but defined below.

// note indentation

template<typename _Formal_argument>

void

public_template() const throw();

template<typename _Iterator>

void

other_template();

private:

class _Helper;

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; never use "__" in a type name, or

// within any name; never use "__[0-9]".

#endif /* _HEADER_ */

namespace std {

template<typename T> // notice: "typename", not "class", no space

long_return_value_type<with_many, args>

function_name(char* pointer, // "char *pointer" is wrong.

char* argument,

const Reference& ref)

{

// int a_local; /* wrong; see below. */

if (test)

{

nested code

}

int a_local = 0; // declare variable at first use.

// char a, b, *p; /* wrong */

char a = 'a';

char b = a + 1;

char* c = "abc"; // each variable goes on its own line, always.

// except maybe here...

for (unsigned i = 0, mask = 1; mask; ++i, mask <<= 1) {

// ...

}

}

gribble::gribble()

: _M_private_data(0), _M_more_stuff(0), _M_helper(0);

{ }

inline int

gribble::three_lines()

{

// doesn't fit in one line.

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有