#include <iostream>
int main()
{
int value1 = 1, value2 = 10;
std::cout << "Min = " << std::min(value1,value2) <<std::endl;
std::cout << "Max = " << std::max(value1,value2)<< std::endl;
}
As far as I know, the min
and max
functions are defined in <algorithm>
.
If I didn't tell the pre-processor to include <algorithm>
why does the code still work?
Most likely, something inside of
iostream
has directly or indirectly included some other header that definesstd::min
andstd::max
. (Perhapsalgorithm
itself has been included. Perhaps some internal header that is used to implement your C++ standard library.)You should not rely on this behavior. Include
algorithm
if you want std::min and std::max.If you are used to a language with a module system where modules can import other modules and not be forced to export anything from their imports (e.g., Racket's module system), this behavior can be confusing.
Recall, however, that #include is doing textual substitution. When the #include line is processed, it is removed from the .cpp file and replaced with the contents of the file it was pointing to.
Most compilers have an option to dump the output of running the preprocessor so you can track down what is including what. You said in your comment to kmort's answer that you are using Visual Studio Express. The command line to preprocess a file to a file using the Visual C++ compiler is
cl /P foo.cpp
. Using this output, we can find that the definition ofstd::max
is coming from the implementation-specific headerxutility
. (Placing the caret inside of the text "std:max" and pressing F12 in Visual Studio is even faster. :-] )kmort also mentioned the
/showIncludes
compiler switch. Using that, we can easily track down the include chain. Here's the reduced output from my run.What compiler are you using?
I have seen compilers before that are somewhat "forgiving" for common items that are defined in libc or libstdc++. It will pull in the references for you. In other words, you don't have to tell it to link with it, nor include the header. It just works. While I would not have expected this of
min()
andmax()
, it's not too surprising.This can also happen by some other header including the one you should be including, but this should not be relied on. And I don't expect it to have happened in this case.