]]>解读google C++ code style谈对C++的理?/title>http://www.shnenglu.com/converse/archive/2010/05/29/116689.html那谁那谁Sat, 29 May 2010 12:34:00 GMThttp://www.shnenglu.com/converse/archive/2010/05/29/116689.htmlhttp://www.shnenglu.com/converse/comments/116689.htmlhttp://www.shnenglu.com/converse/archive/2010/05/29/116689.html#Feedback30http://www.shnenglu.com/converse/comments/commentRss/116689.htmlhttp://www.shnenglu.com/converse/services/trackbacks/116689.html 世界上有很多问题, Z知道如何去解?但是, gq还不算是最高明?更高明的做法是学会避免问题的发生.而如何避免问题的发生, 需要经验的U篏--曄犯下错误,吃一堑长一?于是知道哪些事情是不该做的或者是不应该这么做?
google C++ code style是google对外公布的一份google内部~写C++的代码规范文?与其他很多我曄看过的编码文档一?里面有一些关于代码风格的规定,也就是代码的外观,q一部分不在q里q多讨论,毕竟代码如何才叫"观"是一个见仁见智的话题.在这里专门讨份文档中对一些C++Ҏ该如何使用的讨?最后再做一个ȝ.注意其中的序号ƈ不是文档中的序号,如果要详l了?可以自己ȝq䆾文档.
1) Static and Global Variables Static or global variables of class type are forbidden: they cause hard-to-find bugs due to indeterminate order of construction and destruction.
google明确止全局对象是类对象, 只能是所谓POD(Plain Old Data,如int char{?数据才行.因ؓC++标准中没有明规定全局对象的初始化序, 假设全局cd象A,B,其中A的初始化依赖于B的? 那么无法保证最后的l果.如果非要使用全局cd? 那么只能使用指针, 在main{函数入口统一q行初始?
2) Doing Work in Constructors In general, constructors should merely set member variables to their initial values. Any complex initialization should go in an explicit Init() method.
There is no easy way for constructors to signal errors,
short of using exceptions (which are
forbidden).
If the work fails, we now have an object whose
initialization code failed, so it may be an
indeterminate state.
If the work calls virtual functions, these calls will
not get dispatched to the subclass implementations.
Future modification to your class can quietly introduce
this problem even if your class is not currently
subclassed, causing much confusion.
If someone creates a global variable of this type
(which is against the rules, but still), the
constructor code will be called before
main(), possibly breaking some implicit
assumptions in the constructor code. For instance,
gflags
will not yet have been initialized.
3) Default Constructors You must define a default constructor if your class defines member variables and has no other constructors. Otherwise the compiler will do it for you, badly.
7) Function Overloading Use overloaded functions (including constructors) only in cases where input can be specified in different types that contain the same information. Do not use function overloading to simulate default function parameters.
When you add a throw statement to an existing
function, you must examine all of its transitive callers.
Either
they must make at least the basic exception safety guarantee,
or
they must never catch the exception and be happy with the
program terminating as a result. For instance, if
f() calls g() calls
h(), and h throws an exception
that f catches, g has to be
careful or it may not clean up properly.
More generally, exceptions make the control flow of
programs difficult to evaluate by looking at code: functions
may return in places you don't expect. This results
maintainability and debugging difficulties. You can minimize
this cost via some rules on how and where exceptions can be
used, but at the cost of more that a developer needs to know
and understand.
Exception safety requires both RAII and different coding
practices. Lots of supporting machinery is needed to make
writing correct exception-safe code easy. Further, to avoid
requiring readers to understand the entire call graph,
exception-safe code must isolate logic that writes to
persistent state into a "commit" phase. This will have both
benefits and costs (perhaps where you're forced to obfuscate
code to isolate the commit). Allowing exceptions would force
us to always pay those costs even when they're not worth
it.
Turning on exceptions adds data to each binary produced,
increasing compile time (probably slightly) and possibly
increasing address space pressure.
The availability of exceptions may encourage developers
to throw them when they are not appropriate or recover from
them when it's not safe to do so. For example, invalid user
input should not cause exceptions to be thrown. We would
need to make the style guide even longer to document these
restrictions!
上面提到的理׃, 我认Z用异常最大的宛_是:异常的用导致了E序无法按照代码所展现的流E去走的, 比如代码里面写了步骤一二三,但是假如有异常出? q就不好预知代码真正步进的步骤了, 在出现问题时, l调试和跟踪带来困难. 另外, 我更喜欢unix API的设?熟悉unix~程的h都知? unix API基本上都遵守下列规则: a) q回0表示成功, 其他(一般是-1)表示p|. b) 在失败时, 可以Ҏerrno判断p|的原? q些在man手册中都是会清楚的描q?
ȝ一? q䆾规范中规避的C++Ҏ大致分Z下几c? a) 避免使用那些没有定行ؓ的特?如全局变量不能是类对象(初始化顺序不定), 不用编译器生成的默认构造函?构造行Z定), 异常(代码走向不确?. b) 避免使用那些隐式发生的操?如声明单参数构造函Cؓexplict以避免隐式{? 不定义拷贝构造函数避免隐式的拯行ؓ, 不用操作符重蝲避免隐式的{?br>c) Ҏ׃可的Ҏ给予明的规定:不用函数重载而是定义Ҏ个类型明的函数. d) 即出错了程序也有办法知? 比如不能在类构造函Cq行复杂的构造操? 这些移动到cInit()的函C.
]]>memcache内存池的设计原理http://www.shnenglu.com/converse/archive/2008/01/21/41592.html那谁那谁Mon, 21 Jan 2008 15:34:00 GMThttp://www.shnenglu.com/converse/archive/2008/01/21/41592.htmlhttp://www.shnenglu.com/converse/comments/41592.htmlhttp://www.shnenglu.com/converse/archive/2008/01/21/41592.html#Feedback1http://www.shnenglu.com/converse/comments/commentRss/41592.htmlhttp://www.shnenglu.com/converse/services/trackbacks/41592.html
typedef struct { unsigned int size; /* sizes of items */ unsigned int perslab; /* how many items per slab */ void **slots; /* list of item ptrs */ unsigned int sl_total; /* size of previous array*/ unsigned int sl_curr; /* first free slot */ void *end_page_ptr; /* pointer tonext free item at end of page, or0*/ unsigned int end_page_free; /* number of items remaining at end of last alloced page */ unsigned int slabs; /* how many slabs were allocated for this class */ void **slab_list; /*array of slab pointers */ unsigned int list_size; /* size of prev array*/ unsigned int killing; /* index+1 of dying slab, or zero if none */ } slabclass_t;
]]>Modern C++ Design(MCD)学习W记 && 试代码(?http://www.shnenglu.com/converse/archive/2007/02/04/18340.html那谁那谁Sun, 04 Feb 2007 15:18:00 GMThttp://www.shnenglu.com/converse/archive/2007/02/04/18340.htmlhttp://www.shnenglu.com/converse/comments/18340.htmlhttp://www.shnenglu.com/converse/archive/2007/02/04/18340.html#Feedback5http://www.shnenglu.com/converse/comments/commentRss/18340.htmlhttp://www.shnenglu.com/converse/services/trackbacks/18340.html阅读全文
]]>Modern C++ Design(MCD)学习W记 && 试代码(一)http://www.shnenglu.com/converse/archive/2007/02/04/18325.html那谁那谁Sun, 04 Feb 2007 07:53:00 GMThttp://www.shnenglu.com/converse/archive/2007/02/04/18325.htmlhttp://www.shnenglu.com/converse/comments/18325.htmlhttp://www.shnenglu.com/converse/archive/2007/02/04/18325.html#Feedback1http://www.shnenglu.com/converse/comments/commentRss/18325.htmlhttp://www.shnenglu.com/converse/services/trackbacks/18325.html阅读全文