C++ Logo

STD-DISCUSSION

Advanced search

Subject: Member function, using, and ADL
From: Yongwei Wu (wuyongwei_at_[hidden])
Date: 2021-05-29 09:35:25


I happened to find Alan Griffiths' article ‘C++ Standards - The "swap"
Problem"’ (<URL:https://accu.org/journals/overload/9/41/griffiths_466/>),
and I did some tests while reading it. I was happy and surprised to find
what he described seemed a non-issue now.

I tested with the following code:

#include <iostream>
#include <utility>

namespace Impl {

struct A {
    int value;
};

void swap(A& x, A& y)
{
    std::cout << "Impl::A::swap\n";
    std::swap(x, y);
}

}

namespace App {

struct Obj {
    void swap(Obj& rhs);
    Impl::A a_value;
};

void Obj::swap(Obj& rhs)
{
    using std::swap; // NB this line
    swap(a_value, rhs.a_value);
}

void swap(Obj& lhs, Obj& rhs)
{
    lhs.swap(rhs);
}

}

int main()
{
    App::Obj a, b;
    swap(a, b);
}

I have found out that an error will occur without the ‘using std::swap;’
line, but with it ADL will be in effect and find Impl::A::swap.

The interesting thing is that I do not need to write `using Impl::swap;`—it
seems any using of non-member functions will enable ADL. But I do not know
why.

I have looked up the C++ standard and the cppreference.com web site, but
failed to find the exact paragraph that dictates this behaviour. Anyone
here knows which rules in C++ make this possible?

Thanks and best regards,

-- 
Yongwei Wu
URL: http://wyw.dcweb.cn/


STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups