tags:

views:

327

answers:

3

Hello,

I have my program written in C++ and it is can be successfully compiled on Ubuntu 9.04 with g++ 4.3.4 and Solaris OS with g++ 3.4.3. Now I have upgraded my Ubuntu to version 9.10 and g++ to version 4.4.1. Now compiler invokes the error in STL.

/usr/include/c++/4.4/bits/stl_deque.h: In member function ‘void std::deque<_Tp, _Alloc>::swap(std::deque<_Tp, _Alloc>&)’:
In file included from /usr/include/c++/4.4/deque:65,
/usr/include/c++/4.4/bits/stl_deque.h:1404: error: ‘swap’ is not a member of ‘std’
/usr/include/c++/4.4/bits/stl_deque.h:1405: error: ‘swap’ is not a member of ‘std’
/usr/include/c++/4.4/bits/stl_deque.h:1406: error: ‘swap’ is not a member of ‘std’
/usr/include/c++/4.4/bits/stl_deque.h:1407: error: ‘swap’ is not a member of ‘std’

I don't know how to fix it and if is possible that stl contains a bug. Can you help me, please?

Thanks a lot for all advices.

+5  A: 
#include <algorithm>
Ignacio Vazquez-Abrams
You are right, thanks a lot! Can you explain me why I have to include algorithm when I am using queue? Why it is not included in default?
Gaim
Oh @palm3D explained it. Still thanks a lot
Gaim
IMO that's plain wrong. (If it is indeed necessary, then there's a bug in the std lib implementation.) Why did this get up-voted?
sbi
@sbi: It *is* correct, even if it is only a workaround for a bug in the STL.
Ignacio Vazquez-Abrams
@Ignacio: Except that dribeas cannot reproduce the error with g++ 4.4.1 - which seems to support my suspicion that something else might be fishy.
sbi
A: 

A post on an Apple forum suggests

#include <algorithm>
Andy Shellam
If you're going to downvote please be courteous as to explain why. If it's because the std::deque file should include algorithm on it's own, then I agree, but it doesn't make this any less of a fix if that's the case, especially as exactly the same answer has received 5 up votes.
Andy Shellam
... and the same answer was accepted!
Andy Shellam
@Andy: It might have been accepted, but I down-voted it just as well - and for exactly the same reason. I know it isn't fair for you, but I have only one vote for each answer. It wasn't me who made the difference..
sbi
Yes it's not "desired" behaviour, but it works and so that doesn't make our answers any less relevant - people are after solutions here and we provided one. So it still stands, why the downvote?
Andy Shellam
@Andy Shellam I voted down for no answer in this question I voted up only and have accepted the first answer which helped me.
Gaim
+1  A: 

In older versions of GCC, if you included any standard library header, that header would usually include many others. As mentioned by others, in your case <algorithm> got included this way.

This behavior isn't required by the standard. Many other implementations of the standard library don't exhibit this behavior, or to a lesser degree. It was just a design decision made by the GCC developers long ago. It seems they are now reducing this behavior, bringing it more in line with other implementations. This gives you more control over what gets included and what doesn't. Old code will break, but it's easily fixed by including the missing headers.

palm3D
-1, _This behavior isn't required_ is misleading: STL headers should be self contained. If you include `<deque>` and use `deque::swap` and that member function requires `std::swap` then the implementor of the STL must include `<algorithm>`. There is no requirement in the standard for user code to manually include `<algorithm>` before using `deque::swap`. If a program includes only `<deque>` and uses `deque::swap` it is well formed, so a compilation error is a problem with the compiler/stl, not with the program. While the inclusion of `<algorithm>` is not required, compiling the code is.
David Rodríguez - dribeas
... my comment is also misleading: the STL does not need to have `<deque>` include `<algorithm>`, it needs to allow all programs that include only `<deque>` and only use `deque` operation compile even if they do not include `<algorithm>`. One option would be moving `std::swap` into a different implementation defined header that gets included from both `<algorithm>` and `<deque>` (and for what matters any other standard header that only requires `std::swap` without actually requiring all `<algorithm>`
David Rodríguez - dribeas
You're right. It seems they've been overzealous in removing "gratuitous" includes :-)
palm3D
Gaim