473,416 Members | 1,737 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,416 software developers and data experts.

What swap is called when using std::swap?

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 the ::swap functions I defined down below be called in the
following main()?

#include <algorithm>
struct Foo {};

void swap(int &, int &) {}
void swap(Foo &, Foo &) {}
template<typename Tvoid swap(T*&, T*&) {}

int main()
{
using std::swap;

int i1, i2;
swap(i1, i2);

int *ptr1, *ptr2;
swap(ptr1, ptr2);

Foo foo1, foo2;
swap(foo1, foo2);

Foo *foo_ptr1, *foo_ptr2;
swap(foo_ptr1, foo_ptr2);
}

To my surprise, MSVC++ 8.0 prefers to call std::swap for int and int*.
It only calls ::swap for Foo and Foo*. But GNU g++ 3.4.4 surprises me
even more, as it never calls any of my ::swap overloads at all, and
always prefers calling std::swap instead! So what's the Standard
compliant way?
Kind regards

Niels Dekker
xs4all.nl/~nd/dekkerware
Jul 19 '06 #1
4 4081

Niels Dekker (no reply address) wrote:
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?
"using std::swap" would mean std::swap would be taken into
consideration when establishing the initial overload set. Usually, when
all aspects of overload resolution are equal, non-templates would be
preferred.
#include <algorithm>
struct Foo {};

void swap(int &, int &) {}
void swap(Foo &, Foo &) {}
template<typename Tvoid swap(T*&, T*&) {}

int main()
{
using std::swap;

int i1, i2;
swap(i1, i2);

I would call you swap(int&,int&) a perfect match here, and would put my
money on it being called everytime.

>
int *ptr1, *ptr2;
swap(ptr1, ptr2);
Your (template) version of swap is more specialized than the std::swap
- therefore it should be called here (I'm using SGI std::swap as
reference).

SGI...
template <class Assignable>
void swap(Assignable& a, Assignable& b);
....
It may be possible that other libs have more specialized swaps for
pointers. If this is the case (IMO), you should get ambiguities.
>
Foo foo1, foo2;
swap(foo1, foo2);
void swap(Foo &, Foo &), for the same reason - non-templates preferred.
>
Foo *foo_ptr1, *foo_ptr2;
swap(foo_ptr1, foo_ptr2);
Once again, your template version is more specialized.

}

To my surprise, MSVC++ 8.0 prefers to call std::swap for int and int*.
This is surprising IMhO, and would like someone to educate me too in
the event of me being wrong :-). I don't consider the compilers std
compliant, but I may be wrong.

Kind regards,

Werner

Jul 19 '06 #2
In article <44***************@this.is.invalid>,
"Niels Dekker (no reply address)" <un*****@this.is.invalidwrote:
Will this ::swap be called, or is std::swap still
preferred? I ask this because the compilers I tried disagree! So will
any of the ::swap functions I defined down below be called in the
following main()?

#include <algorithm>
struct Foo {};

void swap(int &, int &) {}
void swap(Foo &, Foo &) {}
template<typename Tvoid swap(T*&, T*&) {}

int main()
{
using std::swap;

int i1, i2;
swap(i1, i2);

int *ptr1, *ptr2;
swap(ptr1, ptr2);

Foo foo1, foo2;
swap(foo1, foo2);

Foo *foo_ptr1, *foo_ptr2;
swap(foo_ptr1, foo_ptr2);
}

To my surprise, MSVC++ 8.0 prefers to call std::swap for int and int*.
It only calls ::swap for Foo and Foo*. But GNU g++ 3.4.4 surprises me
even more, as it never calls any of my ::swap overloads at all, and
always prefers calling std::swap instead! So what's the Standard
compliant way?
swap(i1, i2);

This calls std::swap. The function-local using declaration hides the
global swaps from ordinary lookup, but not ADL. The global swap isn't
found via ADL because there is no namespace associated with int.

swap(ptr1, ptr2);

This calls std::swap, for the same reasons as the previous call.

swap(foo1, foo2);

This calls ::swap(Foo&,Foo&). Ordinary lookup does not find this, but
ADL kicks in and searches the namespace associated with Foo (global).

swap(foo_ptr1, foo_ptr2);

This calls ::swap(T*&, T*&). Ordinary lookup does not find this, but
ADL kicks in and searches the namespace associated with Foo (global).

An interesting experiment is to move the using declaration to namespace
scope (above main), and reexamine.

-Howard
Jul 19 '06 #3

Howard Hinnant wrote:
This calls std::swap. The function-local using declaration hides the
global swaps from ordinary lookup, but not ADL. The global swap isn't
found via ADL because there is no namespace associated with int.
Interesting. I was under the impression that because main is defined in
the global scope, using declarations in it become associated with the
global scope. This is obviously wrong - it becomes associated with the
function scope, which takes precedence over the global scope. Therefore
the initial overload set considered is that within function scope
(which is what can be found via ADL and what is part of function scope
by local using). I thought I would learn by taking the shot :-)

An interesting experiment is to move the using declaration to namespace
scope (above main), and reexamine.
Quick guess is that it will work iaw. with my original thought, as the
global swaps and std::swap would form part of the same overload set,
and the OP's swaps are more specialized?

Kind regards,

W

Jul 19 '06 #4
Thanks very much to you both, Werner and Howard!
int main()
{
using std::swap;

int i1, i2;
swap(i1, i2);
Howard Hinnant wrote:
This calls std::swap. The function-local using declaration hides the
global swaps from ordinary lookup, but not ADL. The global swap isn't
found via ADL because there is no namespace associated with int.

It's clear to me now :-)
Kind regards,

Niels
Jul 20 '06 #5

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...
5
by: Steve Hill | last post by:
Hi, suppose I have a vector allocated on the heap. Can I use a temporary (stack based vector) and swap to clear it, or is this unsafe. e.g. vector<int> *v; vector<int> tmp; v->swap(tmp); // is...
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...
7
by: Kai-Uwe Bux | last post by:
Hi folks, I am still struggling with the rules for name lookup. Please consider: namespace xxx {
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
1
by: Sung Jin Hwang | last post by:
Hi. Is there a good way to extend std::swap to user type classes? Overloading can be a solution but when user type is not a class but class template, we need partial specialization and function...
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
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...
0
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...

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.