Argument-Dependent Lookup

S

siddhu

Dear experts,

As far as I understood the ADL concept things should work in the
following way.

I have a global ostream operator defined as

//ostreamtest.cpp
#include<iostream>
using namespace std;

ostream& operator<<(ostream& out,const string& str)
{
out<<"in ostream"<<endl;
out<<str.c_str()<<endl;

return out;
}

int main()
{
string s("abcd");
cout<<s<<endl;
}

The output of above program is

in ostream
abcd

That means It is calling global ostream operator. But I expected from
this program to call ostream operator defined in std namespace.
Suggestions would be of great help.

Regards,
Siddharth
 
V

Victor Bazarov

siddhu said:
Dear experts,

As far as I understood the ADL concept things should work in the
following way.

I have a global ostream operator defined as

//ostreamtest.cpp
#include<iostream>
using namespace std;

ostream& operator<<(ostream& out,const string& str)
{
out<<"in ostream"<<endl;
out<<str.c_str()<<endl;

return out;
}

int main()
{
string s("abcd");
cout<<s<<endl;
}

The output of above program is

in ostream
abcd

That means It is calling global ostream operator. But I expected from
this program to call ostream operator defined in std namespace.
Suggestions would be of great help.

Try including <string>. I don't think that without it your program is
well-defined (or at least portable).

V
 
S

siddhu

Try including <string>. I don't think that without it your program is
well-defined (or at least portable).

Even then it produces the same result. I am using g++ compiler.
 
V

Victor Bazarov

siddhu said:
Even then it produces the same result. I am using g++ compiler.

ADL does not prohibit using the function from the global NS or dictates
the way in which overloaded functions are resolved. It only says that
if you didn't have the global operator<< defined, the one from 'std'
would be found.

V
 
J

James Kanze

As far as I understood the ADL concept things should work in the
following way.
I have a global ostream operator defined as
//ostreamtest.cpp
#include<iostream>
using namespace std;
ostream& operator<<(ostream& out,const string& str)
{
out<<"in ostream"<<endl;
out<<str.c_str()<<endl;
return out;
}
int main()
{
string s("abcd");
cout<<s<<endl;
}
The output of above program is
in ostream
abcd
That means It is calling global ostream operator. But I expected from
this program to call ostream operator defined in std namespace.

ADL only kicks in after normal unqualified name lookup, or for
dependent names in templates. Since the compiler finds the
global function during normal unqualified name lookup, it
doesn't pull in the corresponding function during ADL.
(Actually, I'm not too sure about this. The standard says that
the set of functions is the union of those found with
unqualified lookup and those found with ADL. Which sounds to me
as if both functions should be present, and the call should be
ambiguous. All of the compilers I have access to resolve it to
the global function, however.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top