[問題] 谷歌的C++ Style Guide

看板C_and_CPP (C/C++)作者 (rosemary)時間13年前 (2012/11/18 17:29), 編輯推噓0(0010)
留言10則, 4人參與, 最新討論串1/2 (看更多)
最近在study谷歌的C++ Style Guide http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml 有一些看不太懂的地方: ===== Q1 ===== The -inl.h Files: ...However, implementation code properly belongs in .cc files, and we do not like to have much actual code in .h files unless there is a readability or performance advantage... 有聽過類似的建議,不要再header file裡面放太多實作, 實作盡量放在.c file裡,但是不太知道這樣做的好處是什麼??? 谷歌的style guide又提到"unless there is a readability or performance advantage" 但在甚麼樣的情況下,把實作的code搬到header file會提升performance呢??? ===== Q2 ===== Casting: Use C++ casts like static_cast<>(). Do not use other cast formats like int y = (int)x; or int y = int(x);. Pros: The problem with C casts is the ambiguity of the operation; sometimes you are doing a conversion (e.g., (int)3.5) and sometimes you are doing a cast (e.g., (int)"hello"); C++ casts avoid this. Additionally C++ casts are more visible when searching for them. 看不太懂C style的轉型不好的點在哪?? 意思是說C style的轉型太暴力, 連不合乎邏輯的轉型都可以轉的過嗎??? (比方說: (int)"hello") -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.116.153.199 ※ 編輯: rosemary0401 來自: 122.116.153.199 (11/18 17:30)

11/18 17:37, , 1F
因為實作常變動,include該header的人每次更動都要重新編
11/18 17:37, 1F

11/18 17:38, , 2F
譯 反之,如果header不變只要重新連結就可以了
11/18 17:38, 2F

11/18 17:41, , 3F
例外狀況..大概是一些macro,inline或template function吧
11/18 17:41, 3F

11/18 17:45, , 4F
轉型這個是安全問題,(int)x,除非x的型別可以正確轉成int
11/18 17:45, 4F

11/18 17:46, , 5F
否則就是不安全的操作,用C++的cast可以明確的告訴編譯器
11/18 17:46, 5F

11/18 17:48, , 6F
你想幹嘛,這樣它可以幫你檢查是否安全,C的轉型就不行了
11/18 17:48, 6F

11/18 21:24, , 7F
寫在 header 有時還有 linkage 的問題
11/18 21:24, 7F

11/18 21:25, , 8F
另外 xxx_cast 比較好搜尋
11/18 21:25, 8F

11/18 23:50, , 9F
第一條不是所有C語言課本都會講的事情。
11/18 23:50, 9F

11/19 06:19, , 10F
多個問題請整理在一篇發問, 勿再多開新討論串
11/19 06:19, 10F
文章代碼(AID): #1GgAgCHC (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1GgAgCHC (C_and_CPP)