C++ Standard Library Style Guidelines
///This articel just is a guideline but not a rule.此文仅为建议,并为规范。
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.org/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 definitions
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()
Reason: Koenig lookup.
11. Namespaces
namespace std
{
blah blah blah;
} // namespace std
-NOT-
namespace std {
blah blah blah;
} // namespace std
12. Spacing under protected and private in class declarations:
space above, none below
ie
public:
int foo;
-NOT-
public:
int foo;
13. Spacing WRT return statements.
no extra spacing before returns, no parenthesis
ie
}
return __ret;
-NOT-
}
return __ret;
-NOT-
}
return (__ret);
14. Location of global variables.
All global variables of class type, whether in the "user visable"
space (e.g., cin) or the implementation namespace, must be defined
as a character array with the appropriate alignment and then later
re-initialized to the correct value.
This is due to startup issues on certain platforms, such as AIX.
For more explanation and examples, see src/globals.cc. All such
variables should be contained in that file, for simplicity.
15. Exception abstractions
Use the exception abstractions found in functexcept.h, which allow
C++ programmers to use this library with -fno-exceptions. (Even if
that is rarely advisable, it's a necessary evil for backwards
compatibility.)
16. Exception error messages
All start with the name of the function where the exception is
thrown, and then (optional) descriptive text is added. Example:
__throw_logic_error(__N("basic_string::_S_construct NULL not valid"));
Reason: The verbose terminate handler prints out exception::what(),
as well as the typeinfo for the thrown exception. As this is the
default terminate handler, by putting location info into the
exception string, a very useful error message is printed out for
uncaught exceptions. So useful, in fact, that non-programmers can
give useful error messages, and programmers can intelligently
speculate what went wrong without even using a debugger.
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.
}
} // namespace std
格式化代码工具:GNU的indent,sourceforge上的astyle都可以
简单的 风格:
这个是ANSI的 风格:
namespace foospace
{
int Foo()
{
if (isBar)
{
bar();
return 1;
}
else
return 0;
}
}
K&R 风格
namespace foospace {
int Foo() {
if (isBar) {
bar();
return 1;
} else
return 0;
}
}
LINUX 风格
namespace foospace
{
int Foo()
{
if (isBar) {
bar();
return 1;
} else
return 0;
}
}
GNU 风格
namespace foospace
{
int Foo()
{
if (isBar)
{
bar();
return 1;
}
else
return 0;
}
}
JAVA 风格
class foospace {
int Foo() {
if (isBar) {
bar();
return 1;
} else
return 0;
}
}
老式的函数声明:
char *
create_world (x, y, scale)
int x;
int y;
float scale;
{
. . .
}
C++ Standard Library Style Guidelines相关推荐
- C++学习书籍推荐《The C++ Standard Library 2nd》下载
百度云及其他网盘下载地址:点我 编辑推荐 经典C++教程十年新版再现,众多C++高手和读者好评如潮 畅销全球.经久不衰的C++ STL鸿篇巨著 C++程序员案头必 备的STL参考手册 全面涵盖C+ ...
- python standard lib_跟Python Standard Library混个脸熟(一)
跟Python Standard Library混个脸熟(一) [TOC] Python 本身提供了很丰富的功能,怎么学习这些东西一直是我思考的,通读整个 Python3 Standard Libra ...
- (轉貼) C Standard Library (初級) (C/C++)
轉貼自 http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html C Standard Library [Last modifi ...
- 【概念的辨异】—— ISO C 与 POSIX C(C standard library 与 C POSIX library)
ISO C 表示 C Standard Library,也就是 C 标准库. 二者的主要区别在于: POSIX 是 C 标准库的超集(也即是从内容上,C 标准库是 POSIX 库的一部分,POSIX ...
- C++ Standard Library
C++ Standard Library *注:内容主要是对参考1的学习记录,知识点与图片大都来源于该书, 部分知识点与图片来源于参考2.具体参考信息,见最下方参考.* C++98中新支持的语言特性 ...
- Ensure that you have a dependency on the Kotlin standard library
完整log: Kotlin: Cannot access built-in declaration 'kotlin.Array'. Ensure that you have a dependency ...
- 翻译】geosoft C++ Programming Style Guidelines (已翻译完毕,大家看看自己总结出了哪些吧!)...
REF : http://geosoft.no/development/cppstyle.html 个人意译-大家可以随时拍砖讨论心得!^) 24.29 .53. 57 求助 `~` 1 介绍 这份文 ...
- Python Standard Library (中文版) 网络协议
目录 1. 网络协议 1.1. 概览 1.1.1. Internet 时间协议 1.1.2. HTTP 协议 1.2. socket 模块 1.2.0.1. Example 7-1. 使用 socke ...
- Code Style Guidelines for Contributors Reading Notes
Reading Notes: 1.You must handle every Exception in your code in some principled way. (if you are co ...
最新文章
- linux降低屏幕亮度,降低屏幕亮度,减缓眼疲劳 (linux/windows/firefox/android)
- 6种Python字符串反转方式
- 关于软件实施经验分享
- js判断上传文件为图片格式、excel格式
- 斐波那契数列的几种求解方法
- 六、Kali Linux 2 远程控制
- 程序猿面试八股文分享~
- K-mer特征提取one-hot编码
- 华为小程序怎么弄出来_华为应用市场也支持“小程序”了,你get了吗?
- Procreate插画基础网课培训平台有哪些
- webpack优化系列七:首屏加载优化
- 区块链数据服务 - BDS
- shtml 是什么?
- 【人物志】美团前端通道主席洪磊:一位产品出身、爱焊电路板的工程师 1
- shiro权限管理实例
- pd.melt和FacetGrid
- IP摄像机RTSP协议视频平台EasyNVR点击程序启动后闪退问题排查及解决
- 【Python】Yahoo股票时间序列预测
- 数学中的鞍点、驻点、拐点、极值点
- Towards High-Fidelity 3D Face Reconstruction from In-the-Wild Images
热门文章
- hawk -- pacemaker web GUI
- 什么是PCB抄板?主要抄板软件有哪些?
- 重学计算机网络(二) - 曾记否,查IP地址
- 中国android手机市场占有率,Android成全球第二大手机系统 市场份额达26%
- 利用MATLAB绘制各种表白心形-跳动心形
- Spring : JdbcTemplate操作数据库(详细)
- 员工管理:人才九宫格,提低扩中保高
- 电脑坐姿:经常使用电脑的人士注意了!
- Android Label 标签
- fiilt1左耳无法同步_【FIIL T1 蓝牙耳机使用总结】功能|操作|闪连|防水|音质_摘要频道_什么值得买...