K
Kai-Uwe Bux
Hi folks,
I am still struggling with the rules for name lookup.
Please consider:
namespace xxx {
struct empty {};
void swap ( empty & a, empty & b ) {}
template < typename T >
struct stupid {
T data;
void swap ( stupid & other ) { // line 12
swap( this->data, other.data ); // line 13
}
};
}
int main ( void ) {
xxx::stupid< xxx::empty > a, b;
a.swap(b); // line 22
}
The compiler complains:
In member function 'void xxx::stupid<T>::swap(xxx::stupid<T>&)
[with T = xxx::empty]':
file.cc:22: instantiated from here
file.cc:13: error: no matching function for call to
'xxx::stupid<xxx::empty>:
:swap(xxx::empty&, xxx::empty&)'
file.cc:12: note: candidates are: void xxx::stupid<T>::swap(xxx::stupid<T>&)
[with T = xxx::empty]
Obviously, the presence of the local swap-method in stupid<T> prevents the
compiler from looking outside for other possible swaps. I was under the
impression that the namespace where T (in this case xxx::empty) is defined
would be searched for a match. But that apparently does not happen. Why is
that?
Best
Kai-Uwe Bux
I am still struggling with the rules for name lookup.
Please consider:
namespace xxx {
struct empty {};
void swap ( empty & a, empty & b ) {}
template < typename T >
struct stupid {
T data;
void swap ( stupid & other ) { // line 12
swap( this->data, other.data ); // line 13
}
};
}
int main ( void ) {
xxx::stupid< xxx::empty > a, b;
a.swap(b); // line 22
}
The compiler complains:
In member function 'void xxx::stupid<T>::swap(xxx::stupid<T>&)
[with T = xxx::empty]':
file.cc:22: instantiated from here
file.cc:13: error: no matching function for call to
'xxx::stupid<xxx::empty>:
:swap(xxx::empty&, xxx::empty&)'
file.cc:12: note: candidates are: void xxx::stupid<T>::swap(xxx::stupid<T>&)
[with T = xxx::empty]
Obviously, the presence of the local swap-method in stupid<T> prevents the
compiler from looking outside for other possible swaps. I was under the
impression that the namespace where T (in this case xxx::empty) is defined
would be searched for a match. But that apparently does not happen. Why is
that?
Best
Kai-Uwe Bux