473,663 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::stup id<T>&)
[with T = xxx::empty]':
file.cc:22: instantiated from here
file.cc:13: error: no matching function for call to
'xxx::stupid<xx x::empty>:
:swap(xxx::empt y&, xxx::empty&)'
file.cc:12: note: candidates are: void xxx::stupid<T>: :swap(xxx::stup id<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 1720
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::stup id<T>&)
[with T = xxx::empty]':
file.cc:22: instantiated from here
file.cc:13: error: no matching function for call to
'xxx::stupid<xx x::empty>:
swap(xxx::empty &, xxx::empty&)' file.cc:12: note: candidates are: void
xxx::stupid<T>: :swap(xxx::stup id<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::s wap', 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::s wap', 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_swa p" 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.********@com Acast.net> wrote in message
news:af******** ************@co mcast.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
7827
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 anything that said that swapping strings was guaranteed to not throw though. But it would seem that a constant time swap ought not to throw though. I can't really see a way to implement one that does throw without deliberately sabotaging it at...
8
3943
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
1977
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 objects of your type have a way to exchange their values more efficiently than via brute-force assignment, such as if they have their own swap or equivalent function. Additionally, consider specializing std::swap for your own template types:" ...
4
4094
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 function provided, whose parameter type matches closer to the type of a and b than any of the std::swap overloads does. Will this ::swap be called, or is std::swap still preferred? I ask this because the compilers I tried disagree! So will any of...
9
6190
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
28
2839
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 apply. Can somebody tell me why? Thanks, Jess
1
1802
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, RandomAccessIterator _Last, BinaryPredicate _Comp, IterSwap _Swap );
11
2921
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
876
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 does not call the setter. template <typename T> void swap(T% a, T% b) { T t = a; a = b; b = a; }
0
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.