Argument-Dependent Lookup

S

siddhu

Dear experts,

As far as I understood the ADL concept things shoul work in the
followin 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()
{

}
 
O

Old Wolf

As far as I understood the ADL concept things shoul work in the
followin way.

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

return out;
}

This function will never be found for an expression like:
foo << bar;

because of ADL ! The arguments are all in namespace std,
so the compiler only searches namespace std. It doesn't
find your function that's in the global namespace.

Note that there is nothing you can do about this; trying
to add your function to namespace std would cause
undefined behaviour.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top