Weired compilation with STL <algorithm> header.

W

Wei

Hi all,

I found out I can use the max function which is defined in STL
<algorithm> without including this header in my program. The compilers
I used are GNU g++ 3.4.4 and Visual Studio C++ 2005.

The program is as follows:

#include <iostream>
//#include <algorithm> <-- I don't have to include <algorithm> in
order to use max.
#include <string>

using namespace std;

int main()
{
string str1("abc"), str2("acb");
cout << max(str1, str2) << endl;
return 0;
}

Does anybody know why is that?

Thanks
Wei
 
S

Sylvester Hesp

Wei wrote :
Hi all,

I found out I can use the max function which is defined in STL
<algorithm> without including this header in my program. The compilers
I used are GNU g++ 3.4.4 and Visual Studio C++ 2005.

The program is as follows:

#include <iostream>
//#include <algorithm> <-- I don't have to include <algorithm> in
order to use max.
#include <string>

using namespace std;

int main()
{
string str1("abc"), str2("acb");
cout << max(str1, str2) << endl;
return 0;
}

Does anybody know why is that?

Thanks
Wei

Probably because <iostream> or <string> directly or indirectly need
some definitions contained in <algorithm> or any of it's
platform-specific subparts. However, it is not defined that those
headers include <algorithm> (or the definition of std::max for that
matter), so you'd better not depend on it if you want to write portable
applications.

- Sylvester
 
G

Gavin Deane

Hi all,

I found out I can use the max function which is defined in STL
<algorithm> without including this header in my program. The compilers
I used are GNU g++ 3.4.4 and Visual Studio C++ 2005.

The program is as follows:

#include <iostream>
//#include <algorithm> <-- I don't have to include <algorithm> in
order to use max.
#include <string>

using namespace std;

int main()
{
string str1("abc"), str2("acb");
cout << max(str1, str2) << endl;
return 0;

}

Does anybody know why is that?

Probably the obvious answer: in your implementation <string> includes
<algorithm> itself, either directly or indirectly. std::string has
lots of member functions. Perhaps the implementation fo one or more of
std::string's member functions uses things out of <algorithm>.

There are no requirements on whether a standard header includes other
standard headers, and if so, which ones, so you can't rely on this
behaviour. To be safe, always include the headers you need explicitly
rather than relying on one standard header including another. Easier
said than done sometimes though.

Gavin Deane
 
W

Wei

Probably the obvious answer: in your implementation <string> includes
<algorithm> itself, either directly or indirectly. std::string has
lots of member functions. Perhaps the implementation fo one or more of
std::string's member functions uses things out of <algorithm>.

There are no requirements on whether a standard header includes other
standard headers, and if so, which ones, so you can't rely on this
behaviour. To be safe, always include the headers you need explicitly
rather than relying on one standard header including another. Easier
said than done sometimes though.

Gavin Deane- Hide quoted text -

- Show quoted text -

Yeah! You guys are right. I think it is the <iostream> inexplicitly
includes <algorithm>. But it is still strange to me. why is the STL
designed to behave like this?
 
S

sebor

Yeah! You guys are right. I think it is the <iostream> inexplicitly
includes <algorithm>. But it is still strange to me. why is the STL
designed to behave like this?

It's not necessarily designed to behave like this. Implementations
are just allowed to #include whatever header in whatever other header
they choose. Some implementations take advantage of this permission,
others tend not to. For example, the Apache C++ Standard Library
(http://incubator.apache.org/stdcxx/) is especially careful not to
introduce more symbols into scope than is absolutely necessary and
rejects your original program. For portability it's best to avoid
making assumptions about headers declaring more symbols than the
minimum the standard specifies. Unfortunately, exactly what that
is isn't always crystal clear.

FWIW, there is an open issue to clarify the standard in this area:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#343

Even though I am the author of the issue I don't think the resolution
I proposed back in 2001 is the best one we can come up with (people
would essentially need to memorize what headers include what others).
I think codifying the simple rule of thumb I mentioned above and
clarifying any confusion where it exists would be a better approach.
 
W

Wei

Hey,

Thanks for your insightful comments.

More weired thing about GNU g++ 3.4.4 is:

In order to use max function, you don't need to include <algorithm>,
as I mentioned before...

However, if you want to use min function, which is also defined in
<algorithm>, then you have to include the header <algorithm> in your
program.

Here is my program:
#include <iostream>
#include <algorithm> // if you comment out this line, the program
can't be compiled under g++
#include <string>
using namespace std;

int main()
{
cout << "min(10, 20) is " << min(10, 20) << '\n';
cout << "min(20.9, 10.3) is "
<< min(20.9, 10.3) << '\n';
cout << "min(\"abc\", \"ac\") is "
<< min(string("abc"), string("ac")) << '\n';

return 0;
}

for above program, if you replace all "min" by "max", you can compile
it without including <algorithm>.

So... What's the point? Are "min" and "max" defined in different
header file other than <algorithm>? Then <algorithm> include both of
the different headers?
 
R

Ron Natalie

Wei said:
Hi all,

I found out I can use the max function which is defined in STL
<algorithm> without including this header in my program. The compilers
I used are GNU g++ 3.4.4 and Visual Studio C++ 2005.

The program is as follows:

#include <iostream>
//#include <algorithm> <-- I don't have to include <algorithm> in

Unlike C, C++ is free to include standard headers at will from other
standard headers. This is relatively innocuous as everything is
in it's own (std) namespace. C doesn't have namespaces so it has
to be more careful how the global namespace might get polluted.
 
S

Sylvester Hesp

Wei said:
Hey,

Thanks for your insightful comments.

More weired thing about GNU g++ 3.4.4 is:

In order to use max function, you don't need to include <algorithm>,
as I mentioned before...

However, if you want to use min function, which is also defined in
<algorithm>, then you have to include the header <algorithm> in your
program.

Here is my program:
#include <iostream>
#include <algorithm> // if you comment out this line, the program
can't be compiled under g++
#include <string>
using namespace std;

int main()
{
cout << "min(10, 20) is " << min(10, 20) << '\n';
cout << "min(20.9, 10.3) is "
<< min(20.9, 10.3) << '\n';
cout << "min(\"abc\", \"ac\") is "
<< min(string("abc"), string("ac")) << '\n';

return 0;
}

for above program, if you replace all "min" by "max", you can compile
it without including <algorithm>.

So... What's the point? Are "min" and "max" defined in different
header file other than <algorithm>? Then <algorithm> include both of
the different headers?

Please don't top-post, I've reordered your message chronologically.

The point is that this is all unspecified behaviour. One implementation
can choose to do this while another can do something totally different.
You shouldn't count on it either way. If you want to know why your
implementation does what it does, just browse the header files
yourself. But asking it here is offtopic (because not standard C++),
you're probably better off asking in a newsgroup corresponding to your
implementation if you want to know *why* they made that choice.

- Sylvester
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top