473,405 Members | 2,338 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,405 software developers and data experts.

namespace and operator==

The following code does not compoile with gcc-3.2.3

namespace dummy
{
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
// Interface of Foo
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
class Foo
{
public:

friend bool operator==(const Foo& f0,const Foo& f1);
private:
int a_;
};
}
//************************************************** **********
// Implementation of Doo
//************************************************** **********
bool
operator==(const dummy::Foo& f0,const dummy::Foo& f1)
{
return f0.a_ == f1.a_;
}
After a while, I figured that operator== is actually declared in
namespace dummy:: so the compiler error is legitimate. Therefore I can
fix the implementation with:

bool
dummy::operator==(const dummy::Foo& f0,const dummy::Foo& f1)
{
std::cout<<"in dummy::operator=="<<std::endl;
return f0.a_ == f1.a_;
}

But then I am quite surprise that the following does compile:
int
main(int,char**)
{
dummy::Foo f0;
dummy::Foo f1;
return f0 == f1;
}

Because it should not find the operator== since namespace dummy is not
opened. But I am sure that it is the one called (thanks to the cout).

Then I tried to make operator== be in global namespace with
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
// Interface of Foo
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
friend bool ::operator==(const Foo& f0,const Foo& f1);

//************************************************** **********
// Implementation of Doo
//************************************************** **********
bool
operator==(const dummy::Foo& f0,const dummy::Foo& f1)
{
std::cout<<"in ::operator=="<<std::endl;
return f0.a_ == f1.a_;
}

But then it refuses to compile with:
main.C:11: `bool operator==(const dummy::Foo&, const dummy::Foo&)'
should have
been declared inside `::'

So my question is: is my concern legal? Do I really want to have
operator== in namespace dummy? Is it legal that gcc finds that there is
an dummy::operator== even if the namespace is not opened?

Thanks for insights.
--
+-------------------------------------------------+
| Xavier Décoret - Post Doct |
| Graphics Lab (LCS) - MIT |
| mailto: de*****@graphics.lcs.mit.edu |
| home : http://www.graphics.lcs.mit.edu/~decoret|
+-------------------------------------------------+

Jul 19 '05 #1
2 3492
"Xavier Decoret" <de*****@graphics.lcs.mit.edu> wrote...
[...]
So my question is: is my concern legal?
It's legal. Whether it's valid or not is a different question.
In order to answer it, one needs to know what your concern is.
Do I really want to have
operator== in namespace dummy?
I don't know. Do you?
Is it legal that gcc finds that there is
an dummy::operator== even if the namespace is not opened?


Yes, it is. See 3.4.2/1. The call to operator== is a function
call. Namespaces of the operands are considered in the name
lookup along with all visible scopes.

Victor
Jul 19 '05 #2
Xavier Decoret wrote:
The following code does not compoile with gcc-3.2.3

namespace dummy
{
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
// Interface of Foo
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
class Foo
{
public:

friend bool operator==(const Foo& f0,const Foo& f1);
private:
int a_;
};
}
//************************************************** **********
// Implementation of Doo
//************************************************** **********
bool
operator==(const dummy::Foo& f0,const dummy::Foo& f1)
{
return f0.a_ == f1.a_;
}
After a while, I figured that operator== is actually declared in
namespace dummy:: so the compiler error is legitimate. Therefore I can
fix the implementation with:
It is not really declared yet. But yes, the above 'friend' declaration
refers to the 'operator ==' from namespace 'dummy'.
bool
dummy::operator==(const dummy::Foo& f0,const dummy::Foo& f1)
{
std::cout<<"in dummy::operator=="<<std::endl;
return f0.a_ == f1.a_;
}

But then I am quite surprise that the following does compile:
int
main(int,char**)
{
dummy::Foo f0;
dummy::Foo f1;
return f0 == f1;
}

Because it should not find the operator== since namespace dummy is not
opened. But I am sure that it is the one called (thanks to the cout).
The correct 'operator ==' is found by argument-dependent name lookup.
There's no need to "open" any namespaces here, whatever that means.
Then I tried to make operator== be in global namespace with
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
// Interface of Foo
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
friend bool ::operator==(const Foo& f0,const Foo& f1);

//************************************************** **********
// Implementation of Doo
//************************************************** **********
bool
operator==(const dummy::Foo& f0,const dummy::Foo& f1)
{
std::cout<<"in ::operator=="<<std::endl;
return f0.a_ == f1.a_;
}

But then it refuses to compile with:
main.C:11: `bool operator==(const dummy::Foo&, const dummy::Foo&)'
should have
been declared inside `::'
If I'm not mistaken, you cannot refer to an unknown name in a friend
declaration, unless this name is supposed to belong to immediate
enclosing namespace scope. In other words, you friend declaration can
introduce any names that will (or do already) belong to namespace
'dummy', but if you want to "befriend" anything from global scope, that
thing must be already declared in advance. For example, this will compile

namespace dummy { class Foo; };
bool operator==(const dummy::Foo& f0,const dummy::Foo& f1);

namespace dummy
{
class Foo
{
public:
friend bool ::operator==(const Foo& f0,const Foo& f1);
private:
int a_;
};
}

bool operator==(const dummy::Foo& f0,const dummy::Foo& f1)
{
return f0.a_ == f1.a_;
}

int main()
{
dummy::Foo f0;
dummy::Foo f1;
return f0 == f1;
}

So my question is: is my concern legal?
What concern is that?
Do I really want to have operator== in namespace dummy?
Well, you should really ask yourself. It is perfectly legal to have it
there.
Is it legal that gcc finds that there is
an dummy::operator== even if the namespace is not opened?


Yes, it is. Read about argument-dependent name lookup. Sometimes it is
also referred to as Koenig lookup.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #3

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

Similar topics

1
by: Tim Partridge | last post by:
I want operator<< to be a friend function of a class inside a namespace, but I don't know how. For example: #include <iostream> namespace ns { class foo { public: // there is something...
8
by: Jason C | last post by:
Hi, I am coming from C background and trying to learn C++. So bear with me if the answer to my question is obvious. I am trying to overload the "+" operator in different namespaces, like this:...
6
by: SpOiLeR | last post by:
Why doesn't following code compile? Problem line is commented in code. ---------------------- Cut here------------------------------- #include <iostream> #include <list> #include <string> ...
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...
2
by: ryan_melville | last post by:
Hi, Should I put the operator<<() for my class (which is in a namespace) in the namespace or make it global? If I understand the lookup rules correctly: If I make it global, it may be hidden...
2
by: Layton | last post by:
Hi, CPP gurus, How to use friend function cross the namespace? I have the following sample code with operator << overloaded, it's working. The problem the operator << function can't access...
11
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
2
by: dirk | last post by:
Why doesn't the standard allow to overload operator new/delete in my namespace, e.g. namespace Foo { void* operator new( std::size_t sz ); void operator delete( void* address ); } On the...
3
by: mrstephengross | last post by:
Hi folks. I've got a weird situation--gcc doesn't like the folllowing code snippet, but I don't know if it's correct or not. Here's the situation: In the global namespace, I've got a operator<<...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.