473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.
Jul 19 '05 #1
4 1742
On Sat, 25 Oct 2003 18:19:49 GMT, "James W. Walker"
<os*******@jwwalker.com.invalid> wrote:
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.


By putting Foo() into an anonymous namespace, it is hidden from
everything outside that namespace.
--
Bob Hairgrove
No**********@Home.com
Jul 19 '05 #2
In article <3f***************@news.webshuttle.ch>, Bob Hairgrove
<wouldnt_you_like@to_know.com> wrote:
By putting Foo() into an anonymous namespace, it is hidden from
everything outside that namespace.


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.
Jul 19 '05 #3
James W. Walker wrote:
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.


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.

Jul 19 '05 #4
"James W. Walker" <os*******@jwwalker.com.invalid> wrote in message news:<251020031119477341%os*******@jwwalker.com.in valid>...
Can someone please explain to me why the following code gives a compile
error when instantiating the sort algorithm?


I believe this is an effect of Koenig lookup rules.

--
Stephen M. Webb
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: wooks | last post by:
I want to apply 2 changes to the following xml <ROOTSTUB app="appname"/> 1. change the app attribute to an element. 2. put the ROOTSTUB element into a namespace that is passed as a global...
3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
5
by: Adam Barr | last post by:
I have a tag foo that I want to copy unchanged when it is a subtag of bar, so I have a template (x is the namespace for the document): <xsl:template match="x:bar/x:foo"> <xsl:copy>...
3
by: Tjerk Wolterink | last post by:
Hello i have xml code like this: <page:page xmlns:page="namespacefor page"> <page:section> <page:header> <b>Hello</b>There </page:header> <page:content> --- HTML CODE like: <i>Y</i>es i...
12
by: Sammy | last post by:
Hi, my mind is going crazy. I have tried everything I can think of to no avail. I have tried Disable Output Escaping. I tried to think of a way of enclosing the attribute data in a CDATA...
5
by: rossum | last post by:
Either I have found a puzzle, or I am just being stupid. Inside a namespace I have a class with a static const double member. The member is not an int so I cannot initialise it within the class,...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
7
by: Bilal | last post by:
Hello all, I came across this problem while working out the bugs in my identity trasnformation stylesheets but sidestepped it for later to see if there is an easier/better solution. This is...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.