您好,登錄后才能下訂單哦!
這篇文章給大家介紹C++中避免使用類型轉換的原因是什么,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
Casts are a well-known source of errors. Make some optimizations unreliable.
類型轉換是眾所周知的錯誤來源之一。讓某些優化處理無法可靠進行。
Example, bad(反面示例)
double d = 2;
auto p = (long*)&d;
auto q = (long long*)&d;
cout << d << ' ' << *p << ' ' << *q << '\n';
What would you think this fragment prints? The result is at best implementation defined. I got
你認為這段代碼會輸出什么?最好的結果是依賴編譯器實現。我得到的是:
2 0 4611686018427387904
Adding
另外
*q = 666;
cout << d << ' ' << *p << ' ' << *q << '\n';
I got
我得到了
3.29048e-321 666 666
Surprised? I'm just glad I didn't crash the program.
神奇么?程序沒崩潰我已經很高興了。
Note(注意)
Programmers who write casts typically assume that they know what they are doing, or that writing a cast makes the program "easier to read". In fact, they often disable the general rules for using values. Overload resolution and template instantiation usually pick the right function if there is a right function to pick. If there is not, maybe there ought to be, rather than applying a local fix (cast).
寫出類型轉換代碼的程序員通常以為知道自己在做什么,或者類型轉換可以讓代碼更容易理解。實際上,它們經常忽視使用值的一般準則。重載和模板例示通常可以選擇正確的函數,只要這個函數存在。如果沒有,沒準應該準備一個,總會好過使用類型轉換解決問題。
Note(注意)
Casts are necessary in a systems programming language. For example, how else would we get the address of a device register into a pointer? However, casts are seriously overused as well as a major source of errors.
類型轉換在系統級編程中是必要的。例如,不然我們怎么獲得登錄到指針中的派生類類型的設備?然而,類型轉換已經被嚴重地過度使用,從而變成了錯誤的主要來源之一。
Note(注意)
If you feel the need for a lot of casts, there may be a fundamental design problem.
如果你覺得需要大量的類型轉換,可能是你的設計存在根本性問題。
Exception(例外)
Casting to (void) is the Standard-sanctioned way to turn off [[nodiscard]] warnings. If you are calling a function with a [[nodiscard]] return and you deliberately want to discard the result, first think hard about whether that is really a good idea (there is usually a good reason the author of the function or of the return type used [[nodiscard]] in the first place), but if you still think it's appropriate and your code reviewer agrees, write (void) to turn off the warning.
轉換成(void)是被廣泛認可的關閉[[nodiscard]]警告的方法。如果你調用了一個帶有[[nodiscard]]返回值的函數,而且你就是希望放棄處理該結果,首先考慮一下這是否是一個好主意(通常函數的作者或者當初使用[[nodiscard]]返回值類型都有很好的理由),但如果考慮之后你還是覺得沒問題,而且你代碼的評審員這也同意的話,使用(void)關閉該警告。
譯者注:
[[nodiscard]]是C++17中引入的新特性,如果調用了返回值聲明為[[nodiscard]]的運算而沒有處理返回值,C++17鼓勵編譯器發布警告。
Alternatives(其他選項)
Casts are widely (mis) used. Modern C++ has rules and constructs that eliminate the need for casts in many contexts, such as
類型轉換被廣泛地使用。現代C++包含很多場景下消除類型轉換的原則和構造,例如
Use templates
使用模板
Use std::variant
使用std::variant
Rely on the well-defined, safe, implicit conversions between pointer types
依靠指針類型之間經過良好定義的,安全的,顯式類型轉換。
Force the elimination of C-style casts, except when casting a [[nodiscard]] function return value to void.
強制消除C風格類型轉換,除了將[[nodiscard]]函數返回值轉換為void之外。
Warn if there are many functional style casts (there is an obvious problem in quantifying 'many').
如果存在很多功能性的類型轉換(修飾詞“很多”顯然有問題),發布警告。
The type profile bans reinterpret_cast.
類型規則群組禁止使用reinterpret_cast。
Warn against identity casts between pointer types, where the source and target types are the same (#Pro-type-identitycast).
如果目的類型和源類型相同,針對指針類型之間的身份轉換發布警告。
Warn if a pointer cast could be implicit.
如果指針類型轉換可能會隱式發生,發布警告。
關于C++中避免使用類型轉換的原因是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。