Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: All
Oct 9, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Increment and Decrement Operators Should not be Applied to Bool Variables

According to the C++ Standard, you are still allowed to apply operator ++ to a variable of type bool for backward compatibility with old code. However, this practice is deprecated and most likely will not be supported in the future. Therefore, you should not use it in new code. Incrementing a bool always yields a 'true' value, regardless of the original value:
	bool isConnected = true;
	bool isTransmitting = false;
	SOCKET socket = browser.Connect(); 
	if (socket) 
  isConnected++;   // value remains 'true'
  isTransmitting++;   // 'true' value assigned
Keep in mind that applying -- (decrement operator) to a bool is illegal.
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date