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

Cross-namespace name lookup

Is the code below ill formed (because operator >> is defined in different
namespace than class B)?

It fails with VS 2005 Beta. I don't know if I should redesign my code or if
I should find a better compiler?

namespace N1
{
class A { };
std::istream &operator >>(std::istream &, A &);
}

namespace N2
{
class B: public A { };
}

void f()
{
A a;
B b;
cin >> a; // OK
cin >> b; // Error: cannot find a matching operator >> for class B
}

Jul 25 '05 #1
8 1899
Interesting... I guess the search rules don't include namespaces of
base classes... Could you cast the B instance as an A ("cin >> (A)b") ?
Or could you put the operator>>() definition in the std namespace?

--Steve

Jul 25 '05 #2


Marcin Kalicinski wrote:
Is the code below ill formed (because operator >> is defined in different
namespace than class B)?

It fails with VS 2005 Beta. I don't know if I should redesign my code or if
I should find a better compiler?

namespace N1
{
class A { };
std::istream &operator >>(std::istream &, A &);
}

namespace N2
{
class B: public A { };
}

void f()
{
A a;
B b;
cin >> a; // OK
cin >> b; // Error: cannot find a matching operator >> for class B
}


This is not the real code, is it? I copy-pasted this code in VS 2003
and I cannot compile the declaration of B since its base class is in a
different namespace. That is why I doubt this is real code.

The code below compiles:
#include <iostream>

namespace N1
{
class A { };
std::istream &operator >>(std::istream & x, A &){return x;}
}
namespace N2
{
class B: public N1::A { };
}
int main()
{
N1::A a;
N2::B b;
std::cin >> a; // OK
std::cin >> b; // OK

return 0;
}

/dan

Jul 25 '05 #3
> Interesting... I guess the search rules don't include namespaces of
base classes... Could you cast the B instance as an A ("cin >> (A)b") ?
Or could you put the operator>>() definition in the std namespace?


Possibly both of these solve the problem. Unforunately, I probably cannot
use any of them! First, function f in my code is actually a template:

template<class T> void f(T &t) { cin >> t; }

So I cannot make the cast :-(

Second, putting anything in std namespace is undefined behavior, as far as I
remember C++ standard.

The original code again:

namespace N1
{
class A { };
std::istream &operator >>(std::istream &, A &);
}

namespace N2
{
class B: public A { };
}

void f()
{
A a;
B b;
cin >> a; // OK
cin >> b; // Error: cannot find a matching operator >> for class B
}
Jul 25 '05 #4
>Possibly both of these solve the problem. Unforunately, I probably cannot
use any of them! First, function f in my code is actually a template:
template<class T> void f(T &t) { cin >> t; } So I cannot make the cast :-(


Yes you can! :) Here's how:

#include "boost/mpl/is_same.hpp"

struct streamAsNormal { template<typename T> static void go(const
std::istream & i, T & t) { i >> t; } };
struct streamAsBase { template<typename T> static void go(const
std::istream & i, T & t) { i >> (A)t; } };

template<class T> void f(T &t)
{
boost::if<boost::is_same<T, B>::type, streamAsBase,
streamAsNormal>::type::go(cin, t);
}

--Steve

Jul 25 '05 #5
Hi,

Yes, you're right. The code is not real, I wrote it only to illustrate the
real problem found in my program. Of course I forgot the namespace qualifier
in class B definition.

I'm sorry to say that but it really compiles, as you said. So I started
digging deeper in my code and found out that the real problem is different
(see bottom for new code). I'm saying sorry to all the people who answered
my initial post...

// The code illustrating the problem:

namespace N1
{
class A { };
}

namespace N2
{
class B: public N1::A { };
}

namespace N3
{
std::istream &operator >>(std::istream & x, N1::A &){return x;}
}

int main()
{
N1::A a;
N2::B b;
std::cin >> a; // Error
std::cin >> b; // Error
return 0;
}

I would like to move operator >> to namespace N1 very much, but I cannot
because namespace N1 is actually namespace std, and class N1::A is
std::vector. Maybe the whole idea of defining stream operations for std
containers is bad?
Jul 25 '05 #6
See my comment before main()
Marcin Kalicinski wrote:
Hi,

Yes, you're right. The code is not real, I wrote it only to illustrate the
real problem found in my program. Of course I forgot the namespace qualifier
in class B definition.

I'm sorry to say that but it really compiles, as you said. So I started
digging deeper in my code and found out that the real problem is different
(see bottom for new code). I'm saying sorry to all the people who answered
my initial post...

// The code illustrating the problem:

namespace N1
{
class A { };
}

namespace N2
{
class B: public N1::A { };
}

namespace N3
{
std::istream &operator >>(std::istream & x, N1::A &){return x;}
}

// let the compiler know where else to look up the names
using namespace N3;

int main()
{
N1::A a;
N2::B b;
std::cin >> a; // Error
std::cin >> b; // Error
return 0;
}

I would like to move operator >> to namespace N1 very much, but I cannot
because namespace N1 is actually namespace std, and class N1::A is
std::vector. Maybe the whole idea of defining stream operations for std
containers is bad?


it should compile now.

/dan

Jul 26 '05 #7
Marcin Kalicinski wrote:
Hi,

Yes, you're right. The code is not real, I wrote it only to illustrate the
real problem found in my program. Of course I forgot the namespace qualifier
in class B definition.

I'm sorry to say that but it really compiles, as you said. So I started
digging deeper in my code and found out that the real problem is different
(see bottom for new code). I'm saying sorry to all the people who answered
my initial post...

// The code illustrating the problem:

namespace N1
{
class A { };
}

namespace N2
{
class B: public N1::A { };
}

namespace N3
{
std::istream &operator >>(std::istream & x, N1::A &){return x;}
}

int main()
{
N1::A a;
N2::B b;
std::cin >> a; // Error
std::cin >> b; // Error
return 0;
}

I would like to move operator >> to namespace N1 very much, but I cannot
because namespace N1 is actually namespace std, and class N1::A is
std::vector. Maybe the whole idea of defining stream operations for std
containers is bad?


std::vector doesn't have a virtual destructor, are you sure you want to inherit from it?

Perhaps you should compose B from a std::vector, might solve your problems, too.

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 27 '05 #8

"Dan Cernat" <dc*****@excite.com> дÈëÓʼþ
news:11**********************@g44g2000cwa.googlegr oups.com...
See my comment before main()
Marcin Kalicinski wrote:
Hi,

Yes, you're right. The code is not real, I wrote it only to illustrate the real problem found in my program. Of course I forgot the namespace qualifier in class B definition.

I'm sorry to say that but it really compiles, as you said. So I started
digging deeper in my code and found out that the real problem is different (see bottom for new code). I'm saying sorry to all the people who answered my initial post...

// The code illustrating the problem:

namespace N1
{
class A { };
}

namespace N2
{
class B: public N1::A { };
}

namespace N3
{
std::istream &operator >>(std::istream & x, N1::A &){return x;}
}

// let the compiler know where else to look up the names
using namespace N3;

int main()
{
N1::A a;
N2::B b;
std::cin >> a; // Error
std::cin >> b; // Error
return 0;
}

I would like to move operator >> to namespace N1 very much, but I cannot
because namespace N1 is actually namespace std, and class N1::A is
std::vector. Maybe the whole idea of defining stream operations for std
containers is bad?


The idea is no bad, but the namespace look up mechanism can't auto find
operators.
Giving thee using namespace is OK when using it.
You indeed expect the operators can be found automatically?


it should compile now.

/dan


No, it doesn't pass still.
I think the compile can't find the operator>> in the statement
std::cin,isn't it.
Wisdo Tang
Aug 3 '05 #9

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

Similar topics

12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
3
by: rollasoc | last post by:
Hi, Doing a bit of system testing on a Windows 98 laptop. (.Net 1.1 app). Did a bit of testing. Loaded a previously saved file. A gray box appeared with the text and buttons all white...
4
by: David Peach | last post by:
Hello, hope somebody here can help me... I have a query that lists defects recorded in a user defined date range. That query is then used as the source for a Cross Tab query that cross-tabs count...
23
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the...
7
by: Scott M. | last post by:
How can I disable the cross-site scripting check for one particular page of a site?
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
3
by: jlamanna | last post by:
I was wondering if there was a utility that could tell you when your C# application is making cross-apartment COM calls. I have a fairly large application that makes extensive use of a 3rd party...
6
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have...
6
by: Bart Van der Donck | last post by:
Hello, I'm presenting my new library 'AJAX Cross Domain' - a javascript extension that allows to perform cross-domain AJAX requests. http://www.ajax-cross-domain.com/ Any comments or...
6
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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...
0
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...
0
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,...

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.