Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Argument-Dependent Lookup

Reply
Thread Tools

Argument-Dependent Lookup

 
 
siddhu
Guest
Posts: n/a
 
      05-14-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-14-2007
siddhu wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
siddhu
Guest
Posts: n/a
 
      05-14-2007
On May 14, 4:12 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> siddhu wrote:
> > 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).


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

>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask- Hide quoted text -
>
> - Show quoted text -



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-14-2007
siddhu wrote:
> On May 14, 4:12 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> siddhu wrote:
>>> 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).

>
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-15-2007
On May 14, 9:58 pm, siddhu <(E-Mail Removed)> 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:(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you tell a Cisco router NOT to cache a DNS lookup? Terra Dactyl Cisco 5 01-07-2005 04:37 AM
Domain Lookup Fred Atkinson Cisco 4 11-16-2004 03:24 PM
No Lookup Fred Atkinson Cisco 2 10-30-2004 02:23 PM
Re: Lookup SSID Paul E Wireless Networking 0 09-01-2004 05:37 PM
string condition file - lookup logic jason@cyberpine.com Perl 0 08-10-2004 07:28 PM



Advertisments