473,406 Members | 2,707 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,406 software developers and data experts.

ambient swap template not found.

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
Oct 8 '05 #1
7 1704
Kai-Uwe Bux wrote:
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.


No, it does not prevent it from looking. It prevents the compiler
from seeing it.
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?

It's called "name hiding", I believe. 'swap' name in the struct
'stupid' scope _hides_ the one in the namespace scope, so while
you're in 'xxx::stupid::swap', the other one is simply invisible.

V
Oct 8 '05 #2
Victor Bazarov wrote:
Kai-Uwe Bux wrote:
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
}

};

}
[snip]
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?

It's called "name hiding", I believe. 'swap' name in the struct
'stupid' scope _hides_ the one in the namespace scope, so while
you're in 'xxx::stupid::swap', the other one is simply invisible.

Thanks a lot for the explanation. However, that leaves me with a problem:
how to tell the template stupid<T> which swap to use. I considered:

template < typename T >
struct stupid {

T data;

void swap ( stupid & other ) {
std::swap( this->data, other.data );
}

};

This, I think, will not find specialized/overloaded version of swap for
types not declared in namespace std; and I cannot dump my stuff in there.

Thus, I did:

namespace xxx {

struct empty {};

void swap ( empty & a, empty & b ) {}

template < typename T >
void global_swap ( T & a, T & b ) {
swap( a, b );
}

template < typename T >
struct stupid {

T data;

void swap ( stupid & other ) {
global_swap( this->data, other.data );
}

};

}

This will find the appropriate swap by looking at the argument type. But
introducing the forwarding function "global_swap" seems clumsy. I would
appreciate suggestions for how to improve upon this.
Thanks again

Kai-Uwe Bux

Oct 8 '05 #3
Kai-Uwe Bux wrote:
[..]
Thanks a lot for the explanation. However, that leaves me with a
problem: how to tell the template stupid<T> which swap to use. I
considered:

template < typename T >
struct stupid {

T data;

void swap ( stupid & other ) {
std::swap( this->data, other.data );
}

};

This, I think, will not find specialized/overloaded version of swap
for types not declared in namespace std; and I cannot dump my stuff
in there.
Yes, you can.

"17.4.3.1 Reserved names
[...]. A program may add template specializations for any
standard library template to namespace std. [...]"

So, you're totally allowed to specialise 'swap' for your class 'empty'
and put it in 'std' namespace.
[...]


V
Oct 9 '05 #4

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:di**********@murdoch.acc.Virginia.EDU...
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
}


how bout xxx::swap instead?

Jon
Oct 9 '05 #5
Jon Slaughter wrote:
"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:di**********@murdoch.acc.Virginia.EDU...
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
}


how bout xxx::swap instead?


That would not allow using 'std::swap' for T other than 'empty'.

V
Oct 9 '05 #6

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:af********************@comcast.com...
Jon Slaughter wrote:
"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:di**********@murdoch.acc.Virginia.EDU...
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
}


how bout xxx::swap instead?


That would not allow using 'std::swap' for T other than 'empty'.

V


Then he needs to specialize stupid::swap if T is of empty type to use
xxx::swap?

basicaly

if T is of type empty then use xxx::swap else use std::swap

or whatever
Oct 9 '05 #7
Victor Bazarov wrote:
Kai-Uwe Bux wrote:
[..]
Thanks a lot for the explanation. However, that leaves me with a
problem: how to tell the template stupid<T> which swap to use. I
considered:

template < typename T >
struct stupid {

T data;

void swap ( stupid & other ) {
std::swap( this->data, other.data );
}

};

This, I think, will not find specialized/overloaded version of swap
for types not declared in namespace std; and I cannot dump my stuff
in there.


Yes, you can.

"17.4.3.1 Reserved names
[...]. A program may add template specializations for any
standard library template to namespace std. [...]"

So, you're totally allowed to specialise 'swap' for your class 'empty'
and put it in 'std' namespace.


Great! Thanks for that crucial piece of information.
Best

Kai-Uwe Bux
Oct 9 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Davis King | last post by:
Can I assume that std::swap will not throw when it is used to swap std::string objects? In the standard it says that the swap() std::string member function runs in constant time. I didn't see...
8
by: surrealtrauma | last post by:
if i have circular linked list like: 14->12->10->8->6->4->2->0->// how can i revises it as 0->2->4->6->8->10->12->14->// is there any other method besides swap? Thank you
2
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when...
4
by: Niels Dekker (no reply address) | last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd Edition, by Scott Meyers), what swap is chosen to be called? using std::swap; swap(a, b); Suppose there is a global ::swap...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
1
by: panzhiyong | last post by:
Hi there! I wonder why there is no such a overload version of sort function in STL: template<class RandomAccessIterator, class Pr, class IterSwap> void sort( RandomAccessIterator _First,...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
0
by: christian2.schmidt | last post by:
Hi, I tried to implement the std::swap method on properties (and indexers) without success. The problem is that list calls the getter and returns a copy and the assignment in the swap method...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.