Boost logo

Boost :

From: K. Noel Belcourt (kbelco_at_[hidden])
Date: 2007-11-16 12:27:05


On Nov 16, 2007, at 5:10 AM, Joaquín Mª López Muñoz wrote:

> 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?

Compiles fine with vacpp-8.0, no errors or warnings.

-- Noel


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