On May 14, 9:58 pm, siddhu <siddharth....@gmail.com> wrote:
> 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.)
--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
|