C++ Brain Teasers: Exercise Your Mind

Knatten, Anders Schau

  • 出版商: Pragmatic Bookshelf
  • 出版日期: 2024-07-09
  • 售價: $1,400
  • 貴賓價: 9.5$1,330
  • 語言: 英文
  • 頁數: 140
  • 裝訂: Quality Paper - also called trade paper
  • ISBN: 9798888650516
  • ISBN-13: 9798888650516
  • 相關分類: C++ 程式語言
  • 海外代購書籍(需單獨結帳)

商品描述

C++ is famous for getting all the default behaviors wrong and for sometimes making demons fly out of your nose. Through 25 puzzles, from the useful to the outright weird, we explore some of C++'s most interesting quirks. How does initialization actually work? Do temporaries even exist? Why is +!!"" a valid expression in C++? As you work through each puzzle, you will peel off some of the layers of complexity of C++, getting a fundamental understanding of how the language works. This will help you write better code and recognize issues more easily while debugging.

Each puzzle in the book is a complete, seemingly simple C++ program, but can you figure out the output for each, or will the answers stump you? Most of the programs compile and have deterministic, though sometimes surprising, output. Some might, however, have undefined behavior, where anything can happen, including making demons fly out of your nose! Yet others might have unspecified behavior with output that is not completely deterministic; we just know nothing as bad as nasal demons will happen.

All puzzles are accompanied by detailed discussions, taking a look under the hood at what exactly is going on to help you understand more about C++'s unexpected behaviors, as well as fundamental concepts like initialization, overload resolution, and implicit conversions. For the non-deterministic cases, we'll explore some typical behaviors one can expect to see in practice, what causes them, and why they shouldn't be relied on.

After working through the book, you'll have tools and techniques to help you write better and safer code, and a better understanding of the fundamentals of the language, the background for some of C++'s apparent weirdness, and a better feel for identifying bugs and unsafe code in your own programs.

What You Need:

The book assumes basic knowledge of C++. If you want to run the programs yourself, you can use either a local C++ compiler or the online compilers at https: //godbolt.org.

商品描述(中文翻譯)

C++ 以其對所有預設行為的錯誤處理而聞名,有時甚至會讓惡魔從你的鼻子飛出來。通過 25 個謎題,從有用的到極其奇怪的,我們探索了一些 C++ 最有趣的怪癖。初始化實際上是如何工作的?臨時變量是否存在?為什麼 +!!'' 在 C++ 中是一個有效的表達式?當你解決每個謎題時,你將揭開 C++ 複雜性的一些層面,對語言的運作原理有基本的理解。這將幫助你寫出更好的代碼,並在調試時更容易識別問題。

書中的每個謎題都是一個完整的、看似簡單的 C++ 程序,但你能找出每個謎題的輸出嗎?還是答案會讓你困惑?大多數程序都可以編譯並具有確定性的輸出,儘管有時會有令人驚訝的結果。然而,有些可能會有未定義的行為,任何事情都可能發生,包括讓惡魔從你的鼻子飛出來!還有一些可能會有未指定的行為,其輸出不完全確定;我們只知道不會有像鼻子惡魔那樣糟糕的事情發生。

所有謎題都附有詳細的討論,深入探討了底層的運作原理,幫助你更好地理解 C++ 的意外行為,以及初始化、重載解析和隱式轉換等基本概念。對於非確定性的情況,我們將探討一些在實踐中可能遇到的典型行為,以及引起這些行為的原因,以及為什麼不應依賴它們。

通過閱讀本書,你將獲得幫助你編寫更好、更安全代碼的工具和技巧,對語言的基礎有更好的理解,了解 C++ 看似奇怪的背後原因,並更好地識別自己程序中的錯誤和不安全代碼。

所需材料:

本書假設讀者具備基本的 C++ 知識。如果你想自己運行這些程序,可以使用本地的 C++ 編譯器或在 https://godbolt.org 上的線上編譯器。