Question

#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?

Was it helpful?

Solution

Most likely, something inside of iostream has directly or indirectly included some other header that defines std::min and std::max. (Perhaps algorithm 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 of std::max is coming from the implementation-specific header xutility. (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.

Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\iostream
Note: including file:  C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\istream
Note: including file:   C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\ostream
Note: including file:    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\ios
Note: including file:     C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xlocnum
Note: including file:      C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\streambuf
Note: including file:       C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xiosbase
Note: including file:        C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xlocale
Note: including file:         C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\stdexcept
Note: including file:          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xstring
Note: including file:           C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xmemory0
Note: including file:            C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xutility

OTHER TIPS

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() and max(), 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.

Just to add the above conversations, I stumbled across a similar issue recently. If you execute the min/max calls without including the algorithm header, it'll still run fine.

std::min(value1, value2)

But if you execute it over an initializer list (put all variables in between {...}) like below,

std::min({value1, value2, value3})

the compiler throws an argument mismatch error. This might most likely be because the function overriding couldn't catch template constexpr T max (initializer_list il, Compare comp) which must only be defined inside algorithm.h. As others have answered, the basic template constexpr const T& max (const T& a, const T& b) could've been included somewhere inside of iostream. But I don't think the whole algorithm header file was included because if it was, the above snippet should've worked too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top