Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2004

Tip: Looking for answers? Try searching our database.

namespace not needed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Manne Baum - 10 Jun 2004 15:49 GMT
namespace MyNS {
    struct A {
        int m;
    };

    template<typename T> T plus (const T& lhs, const T& rhs) { T ret(lhs); ret.m += rhs.m; return ret; } ///< + rhs
}

//==============================================================================
// M A I N
//------------------------------------------------------------------------------
void main()
{
    MyNS::A a, b;
    plus(a, b); //why not 'MyNS::' needed ???
}
Carl Daniel [VC++ MVP] - 10 Jun 2004 15:58 GMT
> namespace MyNS {
> struct A {
[quoted text clipped - 3 lines]
> template<typename T> T plus (const T& lhs, const T& rhs) { T
> ret(lhs); ret.m += rhs.m; return ret; } ///< + rhs }

//==========================================================================
====
> // M A I N

//--------------------------------------------------------------------------
----
> void main()
> {
> MyNS::A a, b;
> plus(a, b); //why not 'MyNS::' needed ???
> }

It's called "argument dependent lookup".  Because a and b are instances of a
class in namespace MyNS, the compiler searches MyNS (in addition to the
global namespace) for the definition of plus().

-cd
tom_usenet - 10 Jun 2004 16:45 GMT
>namespace MyNS {
>    struct A {
[quoted text clipped - 12 lines]
>    plus(a, b); //why not 'MyNS::' needed ???
>}

When you call an unqualified function (no namespace:: at the start),
the name is searched for first of all in the scope that the call is in
(in this case both "main" and the global scope), and also in any
namespaces that the parameters reside in (A is from MyNS, so MyNS is
also searched). This is called argument dependent lookup. I think MSVC
didn't properly support it until VC7.0.

Tom
Signature

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Carl Daniel [VC++ MVP] - 10 Jun 2004 17:44 GMT
> This is called argument dependent lookup. I think MSVC
> didn't properly support it until VC7.0.

I think it was 7.1 actually.  It absolutely was not supported in VC6 and
before.

-cd
tom_usenet - 10 Jun 2004 18:33 GMT
>> This is called argument dependent lookup. I think MSVC
>> didn't properly support it until VC7.0.
>
>I think it was 7.1 actually.  It absolutely was not supported in VC6 and
>before.

Except, slightly bizarrely, for operator overloads. e.g.

namespace ns
{
 struct A{};
 bool operator==(A a, A b)
 {
   return true;
 }
}

int main()
{
 ns::A a1, a2;
 a1 == a2;
 return 0;
}

compiles fine on MSVC6.

Tom
Signature

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Carl Daniel [VC++ MVP] - 10 Jun 2004 18:57 GMT
>>> This is called argument dependent lookup. I think MSVC
>>> didn't properly support it until VC7.0.
[quoted text clipped - 3 lines]
>
> Except, slightly bizarrely, for operator overloads. e.g.

Truly bizarre!

-cd
David Olsen - 10 Jun 2004 21:54 GMT
>>>This is called argument dependent lookup. I think MSVC
>>>didn't properly support it until VC7.0.
[quoted text clipped - 3 lines]
>
> Except, slightly bizarrely, for operator overloads. e.g.

I don't think that's bizarre.  If you are going to implement argument
dependent lookup, the first place to do it is when resolving overloaded
operators, since that is the one place where adding a qualifier to the
function name isn't really possible.  When I was working on a C++
compiler many years ago, that's exactly how I did it.

Signature

David Olsen
qg4h9ykc5m@yahoo.com

tom_usenet - 11 Jun 2004 13:03 GMT
>>>>This is called argument dependent lookup. I think MSVC
>>>>didn't properly support it until VC7.0.
[quoted text clipped - 9 lines]
>function name isn't really possible.  When I was working on a C++
>compiler many years ago, that's exactly how I did it.

I agree that that was the rationale, but I still find it "slightly
bizarre"; if you implement the ADL code for operators, and the
standard says it should work for other functions too, why wouldn't you
just reuse the ADL code for functions too?

My only idea is that ADL for non-operator functions was a fairly late
change to the standard. Certainly Andrew Koenig's original ADL
proposal only mentions operators:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1995/N0645.pdf

Tom
Signature

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.