Originally Posted by
Sang-drax
I cannot see that actually happening. Please give an example of an unintentional use of a symbol.
I would think these problems are rare, especially if you are on your toes, however here is an example:
Code:
// Deep in a header in a library somewhere
int count(int* array1, int* array2, int numElements)
{
int sum = 0;
for (int i = 0; i < numElements; i++)
{
sum += array1[i];
sum += array2[i];
}
return sum;
}
// In main.cpp
// Ooops, forgot to #include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int A[] = { 2, 0, 4, 6, 2, 3, 2, -7 };
const int N = sizeof(A) / sizeof(int);
cout << "Number of twos: "
<< count(A, A + N, 2)
<< endl;
}
Result: undefined behavior with no compile errors. Output: 5578235 on my machine.
In fact, in MSVC++ 6.0, even if you don't forget to include the <algorithm> header, it still uses the wrong count method and outputs the large value.
If you had explicitly typed std::count, however, then a compile error would have made you realize that you forgot the correct header, at which point you'd fix it and then the correct function would be called.
If you have two choices, and one has a remote chance of biting you in the butt, but is easier by a few keystrokes to implement, then I would recommend the one that takes a few more keystrokes and a little getting used to, but doesn't have the same dangers as the easier way. That's just my opinion.