Re: C++ Coding Style

Low Zhen Lin <[email protected]> Tue, 23 Dec 2003 10:13:55 +0800
Newsgroups gmane.linux.zynot.devel
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andreas Pokorny wrote:

| Hi,
|
|> Prefer inline functions or templates to macros for code. Prefer
|> macros to inline functions or templates for simple expressions.
|
| Instead of that I would suggest: Use macros only if you want to
| achieve an effect that can not be done with inline template
| functions.

Inlining is turned off for debugging purposes, macros are not...

One thing that inlining cannot do is do stuff in the caller's scope...

|> ~    reinterpret_cast<unsigned int>aFloat; // Avoid! But a good
|> way to access the representation of an object in memory.
|
| reinterpret_cast is a save cast, for example you can not cast away
| const qualifiers, and you can only cast pointers. It acts different
| than casts like static_cast, and it is not as dangerous as a C
| cast. I do not see any reason to officially avoid it.

Reinterpreting raw data as something else constitutes a breakage of
encapsulation... This should be avoided, except maybe for binary I/O.

| What about template Parameters?
|
| 1.template<typename ParamT, typename AllocT> 2.template<typename
| Param, typename Alloc> 3.template<typename param, typename alloc>
| 4.template<typename param_t, typename alloc_t> .. I prefer #1
|
Typedefs like typenames are types nonetheless, so 1 and 2.

Type 1 reminds the coder that it is a typename...

| Declare your destructor non virtual if you want to express, that
| one may not inherit from that class.

I would also put up rather large signs saying 'don't!'... I wish C++
had final, but Strousoup says, if you want it non-overridable, don't
declare it virtual in the first place! ... I can see some optimisation
tricks using final, for example, instead of doing vtable lookups, they
can hardcode the call instead. But the problem is, the function that
one wants to declare final is usually inherited virtual...

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/56Tjv+6a/MPcjnERAqYMAJ9RoSRiMeKK3x8b/uNAeG7QIhnUEgCff1Ec
DAt0ZkRMqIhEGwO7hz90ZeY=
=TgTx
-----END PGP SIGNATURE-----