473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Weired compilation with STL <algorithm> header.

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
7 2587
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Steffen Brinkmann | last post by:
Hi! I tried to modify the transform algorithm in a way that it doesn't take iterators, but a reference to a container class and a value, because Mostly I need to do an operation of a container...
5
by: google | last post by:
Hi All, I'm just getting started learning to use <algorithm> instead of loads of little for loops, and I'm looking for a bit of advice/mentoring re: implementing the following... I have a...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
11
by: Gerald I. Evenden | last post by:
Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3". The following simple program extracted from p.497 & 499 of N.M.Josurris' "The C++ Standard Library ... " (file t.cpp): 1 #include <string>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.