How does a declaration of use reduce names searchable without ADL?

#include <iostream>
#include <string>

class X {};

namespace N
{

std::string to_string(X)
{
    return "foo";
}

void foo()
{
    //using std::to_string; // will break the build if uncommented...
    //using N::to_string;   // ...unless this is uncommented as well
    std::cout << to_string(X()) << std::endl;
}

}

int main()
{
    N::foo();
    return 0;
}

Either I stumbled upon one of the many C ++ - lasso that I do not master, or I miss something obvious here.

How can I using std::to_stringreduce the set of names available during unqualified searches to only those that are accessible via ADL? Although I can guess that the problem may be that it is to_string(X)declared in a different namespace, than X, I cannot help but notice that without using std::to_string, it’s N::to_string(X)just available for N::foo()using the “normal”, intuitive search rules I'm used to.

+4
source share
1

, . , .

foo int to_string;. N::to_string.

+5

Source: https://habr.com/ru/post/1627648/


All Articles