I recently came across an error as a result of a combination of typo, comma-operator, the default value. The term had many brackets and commas. One comma was placed in one bracket too far. This term was still valid C ++ code, but the return value was incorrect. In the simplified version, the error looked like this:
int intValue = MyString.toInt(),16;
The toInt method has a default parameter for number-base (default is 10). The intValue variable will always be 16.
So the question is, is there any style rule to avoid such errors, or C ++ check / compile rules to help find such errors in the code?
EDIT
Ok, I modified the code a bit to make more sense for the comma:
char * MyString("0x42"); int intValue = stringToInt(MyString),16;
PS Please do not blame me for not using std :: string and streams. The code is for simplified demonstration only. :-)
source share