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 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
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
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
"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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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:"
...
|
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...
|
by: ma740988 |
last post by:
Consider:
# include <vector>
# include <iostream>
# include <cstdlib>
# include <ctime>
bool
ispow2i ( double n )
{
| |
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
|
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
);
|
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 )
{
|
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; }
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
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...
| |