Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:39 PM
James W. Walker
Guest
 
Posts: n/a
Default Namespace & template puzzle

Can someone please explain to me why the following code gives a compile
error when instantiating the sort algorithm?

#include <vector>

namespace
{
struct Foo {
int m;
};
}

static bool operator<( const Foo& inOne, const Foo& inTwo )
{
return inOne.m < inTwo.m;
}

static void test()
{
std::vector<Foo> vec;
std::sort( vec.begin(), vec.end() );
}

It's as if it doesn't understand that I have provided operator< for
Foo. But if I just say

Foo x,y; if (x < y) {... }

instead of sorting, there is no problem. If I move the operator< into
the namespace, then it compiles. So, I know how to fix it, but I'd
like to understand it.
  #2  
Old July 19th, 2005, 08:39 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: Namespace & template puzzle

On Sat, 25 Oct 2003 18:19:49 GMT, "James W. Walker"
<osxNOSPAM@jwwalker.com.invalid> wrote:
[color=blue]
>Can someone please explain to me why the following code gives a compile
>error when instantiating the sort algorithm?
>
>#include <vector>
>
>namespace
>{
> struct Foo {
> int m;
> };
>}
>
>static bool operator<( const Foo& inOne, const Foo& inTwo )
>{
> return inOne.m < inTwo.m;
>}
>
>static void test()
>{
> std::vector<Foo> vec;
> std::sort( vec.begin(), vec.end() );
>}
>
>It's as if it doesn't understand that I have provided operator< for
>Foo. But if I just say
>
>Foo x,y; if (x < y) {... }
>
>instead of sorting, there is no problem. If I move the operator< into
>the namespace, then it compiles. So, I know how to fix it, but I'd
>like to understand it.[/color]

By putting Foo() into an anonymous namespace, it is hidden from
everything outside that namespace.


--
Bob Hairgrove
NoSpamPlease@Home.com
  #3  
Old July 19th, 2005, 08:39 PM
James W. Walker
Guest
 
Posts: n/a
Default Re: Namespace & template puzzle

In article <3f9ad689.11546683@news.webshuttle.ch>, Bob Hairgrove
<wouldnt_you_like@to_know.com> wrote:
[color=blue]
> By putting Foo() into an anonymous namespace, it is hidden from
> everything outside that namespace.[/color]

I don't see how that is consistent with what happened. My definition
of operator< for Foo was outside the namespace, and I didn't get a
compile error on that.
  #4  
Old July 19th, 2005, 08:40 PM
Oplec
Guest
 
Posts: n/a
Default Re: Namespace & template puzzle

James W. Walker wrote:
[color=blue]
> I don't see how that is consistent with what happened. My definition
> of operator< for Foo was outside the namespace, and I didn't get a
> compile error on that.[/color]

I'm new to C++, but from what I have read about anonymous namespaces,
whatever is included in an anonymous namespace is only accessable from
within the single translation unit that it is defined in. Maybe that has
something to do with it.

Oplec.

  #5  
Old July 19th, 2005, 08:42 PM
Stephen M. Webb
Guest
 
Posts: n/a
Default Re: Namespace & template puzzle

"James W. Walker" <osxNOSPAM@jwwalker.com.invalid> wrote in message news:<251020031119477341%osxNOSPAM@jwwalker.com.in valid>...[color=blue]
> Can someone please explain to me why the following code gives a compile
> error when instantiating the sort algorithm?[/color]

I believe this is an effect of Koenig lookup rules.

--
Stephen M. Webb
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles