Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Debunking C++ Myths

You're reading from   Debunking C++ Myths Embark on an insightful journey to uncover the truths behind popular C++ myths and misconceptions

Arrow left icon
Product type Paperback
Published in Dec 2024
Publisher Packt
ISBN-13 9781835884782
Length 226 pages
Edition 1st Edition
Arrow right icon
Authors (2):
Arrow left icon
Ferenc Deak Ferenc Deak
Author Profile Icon Ferenc Deak
Ferenc Deak
Alexandru Bolboaca Alexandru Bolboaca
Author Profile Icon Alexandru Bolboaca
Alexandru Bolboaca
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Chapter 1: C++ Is Very Difficult to Learn FREE CHAPTER 2. Chapter 2: Every C++ Program Is Standard-Compliant 3. Chapter 3: There’s a Single C++, and It Is Object-Oriented 4. Chapter 4: The Main() Function is the Entry Point to Your Application 5. Chapter 5: In a C++ Class, Order Must There Be 6. Chapter 6: C++ Is Not Memory-Safe 7. Chapter 7: There’s No Simple Way to Do Parallelism and Concurrency in C++ 8. Chapter 8: The Fastest C++ Code is Inline Assembly 9. Chapter 9: C++ Is Beautiful 10. Chapter 10: There Are No Libraries For Modern Programming in C++ 11. Chapter 11: C++ Is Backward Compatible ...Even with C 12. Chapter 12: Rust Will Replace C++ 13. Index 14. Other Books You May Enjoy

The definition of zero

Zero is unique among numbers. The concept was present in ancient Egypt, and traces of it were found in ancient Babylon as a placeholder in their number system, but it wasn’t treated as a true number at that point.

The ancient Greeks have had some resentment towards it because, regardless that they knew its importance, initially, due to some philosophical constraints, they didn’t use it as a proper number, because not, to be or not to be, but how can nothing be, that was the question in the ancient Agora.

The breakthrough came in India around the 5th century CE when mathematician Brahmagupta defined zero as a number and established rules for its arithmetic use. This concept spread to the Islamic world, notably through the works of Al-Khwarizmi, and then to Europe, where Fibonacci played a key role in its adoption in the 12th century. Thank you, Wikipedia.

Zero has several important properties: it is the additive identity, meaning adding zero to any number leaves the number unchanged. Multiplying any number by zero results in zero, and division by zero is undefined. Zero is an even number and serves as a neutral element on the number line, being neither positive nor negative. In exponents, raising zero to any positive power gives zero, while any non-zero number raised to the power of zero equals one.

These properties make zero fundamental in mathematics, and with this, we can all agree that zero is one of the most important (if not the most important) numbers that ever existed; its place is up right next to π, or e, or i, which we all know is the square root of all evil, or –1.

Now we have presented concrete proof that there is no other number like zero, we also give the following statement: C++ is a unique language. In its latest iteration, as of 2024, at the current time, in C++, there are six different ways to initialize a value to zero, honoring the fact that zero is the most important number. Ever. Just see the following:

int z;
int main()
{
    int z1 = 0;
    int z2(0);
    int z3{0};
    int z4 = {0};
    int z5{};
    int z6();
}

Let’s break it down, line by line, as there are not that many lines:

  • int z; – Here, a global variable, z, of the int type is declared. Since it’s a global variable, it is automatically initialized to 0 by the compiler (if a global int variable isn’t explicitly initialized, it defaults to zero). This is something we can trust.
  • int z1 = 0; – Copy initialization. The z1 variable is declared as an int type and initialized to 0 using copy initialization. This involves assigning the value of 0 to z1 after it is created.
  • int z2(0); – Direct initialization. The z2 variable is declared and initialized to 0 using direct initialization, which involves passing the value of 0 directly to the constructor of the int type. Not that it has any, but you get the idea.
  • int z3{0}; – Brace initialization (uniform initialization). The z3 variable is declared and initialized to 0 using brace initialization. It helps prevent issues such as narrowing conversions and provides a consistent syntax for initializing different types. This is a peculiar initialization, and we will get back to this syntax a bit later in the next chapter.
  • int z4 = {0}; – Copy list initialization. The z4 variable is declared and initialized to 0 using copy list initialization, a combination of copy initialization and brace initialization. It’s similar to z3 but explicitly uses the assignment syntax, and when we talk about simple things such as numbers, there is really no difference.
  • int z5{}; – Value initialization. The z5 variable is initialized using empty braces, {}, known as value initialization. For fundamental types such as int, this results in z5 being initialized to 0. This method is often used to ensure that a variable is zero-initialized without explicitly assigning a value.

Isn’t it beautiful, the amount of consideration that was put into making a variable correspond to the number zero? So, one might just ask: why are the local variables of C++ not initialized to zero (or their default value), just in case?

The answer to this question is partly historical and partly pragmatic. Since C++ is based on C, and C was designed to be as close to the metal (silicone) as possible, the compiler did not waste precious processor cycles to initialize a value to their default value, if at some stage later it was used to set to a different value needed by the programmer. Elementary, dear reader, as one of the most famous detectives would say.

Last but not least, without me providing any more details, I really hope you have recognized the most vexing parse in int z6();.

The “most vexing parse” is a term used to describe a specific issue in C++ involving the declaration of objects that can be misinterpreted by the compiler due to ambiguities in the syntax. It usually arises when you declare a variable using parentheses, which can sometimes be interpreted as a function declaration rather than a variable definition, just like in our specific example.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime