By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,510 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,510 IT Pros & Developers. It's quick & easy.

Weired compilation with STL <algorithm> header.

P: n/a
Wei
Hi all,

I found out I can use the max function which is defined in STL
<algorithmwithout 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 <algorithmin
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

May 24 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Wei wrote :
Hi all,

I found out I can use the max function which is defined in STL
<algorithmwithout 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 <algorithmin
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 <iostreamor <stringdirectly or indirectly need
some definitions contained in <algorithmor 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
May 24 '07 #2

P: n/a
On 24 May, 17:19, Wei <william.wt...@gmail.comwrote:
Hi all,

I found out I can use the max function which is defined in STL
<algorithmwithout 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 <algorithmin
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 <stringincludes
<algorithmitself, 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

May 24 '07 #3

P: n/a
Wei
On May 24, 12:35 pm, Gavin Deane <deane_ga...@hotmail.comwrote:
On 24 May, 17:19, Wei <william.wt...@gmail.comwrote:


Hi all,
I found out I can use the max function which is defined in STL
<algorithmwithout 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 <algorithmin
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 <stringincludes
<algorithmitself, 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 <iostreaminexplicitly
includes <algorithm>. But it is still strange to me. why is the STL
designed to behave like this?
May 24 '07 #4

P: n/a
On May 24, 12:07 pm, Wei <william.wt...@gmail.comwrote:
[...]
Yeah! You guys are right. I think it is the <iostreaminexplicitly
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/wg...ctive.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.

May 24 '07 #5

P: n/a
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 <algorithmin 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 <algorithminclude both of
the different headers?

On May 24, 2:31 pm, "s...@roguewave.com" <s...@roguewave.comwrote:
On May 24, 12:07 pm, Wei <william.wt...@gmail.comwrote:
[...]
Yeah! You guys are right. I think it is the <iostreaminexplicitly
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/wg...ctive.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.

May 25 '07 #6

P: n/a
Wei wrote:
Hi all,

I found out I can use the max function which is defined in STL
<algorithmwithout 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 <algorithmin
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.
May 25 '07 #7

P: n/a
Wei wrote:
On May 24, 2:31 pm, "s...@roguewave.com" <s...@roguewave.comwrote:
>On May 24, 12:07 pm, Wei <william.wt...@gmail.comwrote:
[...]
>>Yeah! You guys are right. I think it is the <iostreaminexplicitly
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/wg...ctive.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.
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 <algorithmin 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 <algorithminclude 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
May 25 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.