473,811 Members | 3,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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==(cons t Foo& f0,const Foo& f1);
private:
int a_;
};
}
//*************** *************** *************** ***************
// Implementation of Doo
//*************** *************** *************** ***************
bool
operator==(cons t 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::end l;
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==(co nst Foo& f0,const Foo& f1);

//*************** *************** *************** ***************
// Implementation of Doo
//*************** *************** *************** ***************
bool
operator==(cons t 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==(cons t 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*****@graphic s.lcs.mit.edu |
| home : http://www.graphics.lc s.mit.edu/~decoret|
+-------------------------------------------------+

Jul 19 '05 #1
2 3523
"Xavier Decoret" <de*****@graphi cs.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==(cons t Foo& f0,const Foo& f1);
private:
int a_;
};
}
//*************** *************** *************** ***************
// Implementation of Doo
//*************** *************** *************** ***************
bool
operator==(cons t 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::end l;
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==(co nst Foo& f0,const Foo& f1);

//*************** *************** *************** ***************
// Implementation of Doo
//*************** *************** *************** ***************
bool
operator==(cons t 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==(cons t 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==(cons t dummy::Foo& f0,const dummy::Foo& f1);

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

bool operator==(cons t 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
2567
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 wrong with the line below, but I don't know how to fix it. I think I need to
8
2176
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: namespace A {
6
1669
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> using namespace std;
20
3874
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 a named namespace namespace foo { class Foo
2
3198
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 if called from within a different namespace and that other namespace has any operator<<() defined. That doesn't seem good.
2
2198
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 private class (myPoint) member x and y directly and I have to use getters to make it work. If there is no namespace, direct access myPoint::x and y is ok. I tested successfully by add friend declaration inside class myPoint. (turn on line CCC, BBB,...
11
2065
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
3133
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 other hand I can overload new/delete per class:
3
2625
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<< declared that will send a vector<Tto a std::ostream. In the "outer" namespace, I've got a operator<< declared that will send a Thing<Tto a std::ostream. In the "outer" namespace, I've got a function "foo" that tries to send a vector<Tto a...
0
9731
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10651
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10405
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.