Boost logo

Boost :

From: Joaquín Mª López Muñoz (joaquin_at_[hidden])
Date: 2007-11-16 07:10:16


John Maddock ha escrito:

> Hmmm, the interesting thing is, I'm having a hard time convincing myself
> that this code is legal, I'm cc'ing our friendly IBM compiler expert in case
> he can shed light on this.
>
> Consider the code again:
>
> #include <algorithm>
>
> template<typename T>
> struct foo
> {
> T t;
> foo():t(0){}
> void swap(foo& x)
> {
> using std::swap;
> swap(t,x.t);
> }
> };
>
> template<typename T>
> void swap(foo<T>& x,foo<T>& y)
> {
> x.swap(y);
> }
>
> int main()
> {
> foo<int> x,y;
> x.swap(y);
> }
>
> In foo::swap the local declaration of the member function "swap" hides any
> occurances of swap in the outer scope, and the using declaration behaves "as
> if" std::swap were imported into the *enclosing namespace*, which would
> still be hidden... and yet I can't believe that this simple idiom isn't
> actually legal ! :-) And indeed all the other compilers I tested do compile
> this OK.

Ouch, didn't think about name hiding... this is why I love C++ :-/

Now I don't know either whether the code is legal or not. Anyway, if I changed
foo::swap to foo::swop the code would be undoubtedly legal then, right?

Noel, would you please try the attached variation?

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk