Originally Posted by
Elysia
Of course you shouldn't do it, but should a compiler actively allow it? I think not.
The compiler should complain and avoid undefined behavior. It's very easy to hide this little warning.
So IMO, it's a very good thing C++ forbid this nonsense, since it IS bad practice not to use headers.
I don't even know where to start dismantling this opinion of yours.
Code:
// C
int main() {
int foo = 12;
bar(foo);
return;
}
void bar(int foo) { ; }
Visual Studio complains starting on warning level 3 with default project setup.
Didn't test it on gcc, but seen my share of "Implicit declaration of function..." warnings. So not sure what do you mean. The compiler complains.
Meanwhile...
Code:
// C++
int main() {
int foo[5] = {1,2,3,4,5};
int bar[5];
int i = 0;
while (i < 5)
bar[i] = foo[i++];
}
Doesn't sound a blip on warning level 4.
So back to your argument:
>> So IMO, it's a very good thing C++ forbid this nonsense, since it IS bad practice not to use headers.
So it is evaluation order dependency and yet I got no warning. In fact the C++ standard says I don't have to have a warning. It's undefined behavior... as it is the result of implicit declaration of functions in C. More, implicit declaration results in a warning, whereas evaluation order dependency doesn't. And on some circumstances the compiler couldn't even if it wanted too. Which is "worst"? Don't answer.
One last small little unimportant thing of absolutely no consequence at all: C99 removed implicit declaration of functions. So... yeah.