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

Language Subtlety or Compiler bug?

In VC7.1 the following code outputs:
00000000
Success!

It would seem that the Y::operator<< is hiding the global operator<<
when it is outputting an object from another namespace but not when it
is outputting an object in the global namespace.

I can't help but think one of these 2 behaviors is a bug in VC7.1, but
which one?

Thanks,

Ned.


=========================================
#include <iostream>

namespace X
{
class Abc1
{
};

}
std::ostream& operator<< (std::ostream& strm, const X::Abc1 *node)
{
return strm << "Success!";
}

X::Abc1 * GetDoc() { return NULL; }

class Abc2
{
};

Abc2 * GetDoc2() { return NULL; }

std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
{
return strm << "Success!";
}

namespace Y
{
std::ostream & operator<<(std::ostream& a, const char& b)
{
return a << "This should not run";
}

void DoTest()
{
std::cout << GetDoc() << "\n";
std::cout << GetDoc2() << "\n";
}
}

int main(int argc, char* argv[])
{
Y::DoTest();
return 0;
}
Jul 22 '05 #1
7 1135

"Ned Harding" <nh******@extendthereach.com> wrote in message
news:34**************************@posting.google.c om...
In VC7.1 the following code outputs:
00000000
Success!

It would seem that the Y::operator<< is hiding the global operator<<
when it is outputting an object from another namespace but not when it
is outputting an object in the global namespace.

I don't see the operator in the Y namespace coming into play at all here.
What makes you think it is? You're not getting "This should not run" as
your output. Plus, the operator in Y uses a reference to a char, not a
pointer - of *any* type!
=========================================
#include <iostream>

namespace X
{
class Abc1
{
};

}
std::ostream& operator<< (std::ostream& strm, const X::Abc1 *node)
{
return strm << "Success!";
}

X::Abc1 * GetDoc() { return NULL; }

class Abc2
{
};

Abc2 * GetDoc2() { return NULL; }

std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
{
return strm << "Success!";
}

namespace Y
{
std::ostream & operator<<(std::ostream& a, const char& b)
{
return a << "This should not run";
}

void DoTest()
{
std::cout << GetDoc() << "\n";
std::cout << GetDoc2() << "\n";
}
}

int main(int argc, char* argv[])
{
Y::DoTest();
return 0;
}


It looks to me like your operator in the global namespace is correctly
getting called for the case where you've passing it an Abc2*. That's normal
overloading (correct?). In the case where you pass an Abc1*, however, your
code apparently isn't seeing the overloaded operator that uses an Abc1*, but
instead is calling the << normally used for a void* pointer.

I'm no expert on why that is, but if I had to guess (which I do :-)), I'd
say it's because there is nothing specifying that you want to use the
operator << that's in the X namespace. I'm not even sure how you would do
that. Perhaps using X:<< ?

-Howard
Jul 22 '05 #2

"Howard" <al*****@hotmail.com> wrote in message
news:sC3Zc.537788$Gx4.147573@bgtnsc04-> I'm no expert on why that is, but if
I had to guess (which I do :-)), I'd
say it's because there is nothing specifying that you want to use the
operator << that's in the X namespace. I'm not even sure how you would do
that. Perhaps using X:<< ?

I meant to say "X::<<" there. (But as I said, it was just a guess. :-))
-Howard

Jul 22 '05 #3
Howard wrote:
"Howard" <al*****@hotmail.com> wrote in message
news:sC3Zc.537788$Gx4.147573@bgtnsc04-> I'm no expert on why that is, but if
I had to guess (which I do :-)), I'd
say it's because there is nothing specifying that you want to use the
operator << that's in the X namespace. I'm not even sure how you would do
that. Perhaps using X:<< ?

I meant to say "X::<<" there. (But as I said, it was just a guess. :-))


I think that :: is allowed only with the name. << is not a name, so it
probably should be X::operator<<. But I am just adding a guess on top
of your guess...

Victor
Jul 22 '05 #4
nh******@extendthereach.com (Ned Harding) wrote:
In VC7.1 the following code outputs:
00000000
Success!

It would seem that the Y::operator<< is hiding the global operator<<
when it is outputting an object from another namespace but not when it
is outputting an object in the global namespace.
Actually the behaviour is correct.
Also the Y::operator<< is irrelevant, the code will behave the same
without it.

[Abbreviated, equivalent code]: #include <iostream>

namespace X { class Abc1 {} };

std::ostream& operator<< (std::ostream& strm, const X::Abc1 *node)
{ return strm << "Success!"; }

class Abc2 { };

std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
{ return strm << "Success!"; }

namespace Y {
void DoTest() {
std::cout << (X::Abc1 *)0 << "\n";
std::cout << (Abc2 *)0 << "\n";
}
}

int main() {
Y::DoTest();
}
The first example looks for:
operator<< (std::ostream &, X::Abc1 *)
in the namespaces 'std' and 'X'. It finds neither.
It doesn't look in the global namespace because none of its
parameter types are in the global namespace.

However there is an implicit conversion (X::Abc1 *) --> (void *)
so it finds std::operator<<(std::ostream &, void *).

The second example looks for:
operator<< (std::ostream &, ::Abc2 *)
in the namespace 'std' and the global namespace. It finds
your std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
from the global namespace.
namespace Y {
std::ostream & operator<<(std::ostream& a, const char& b)
};


Why did you include that function? It has nothing to do with
anything else in this program because you never go
std::cout << (something with char type or convertible to char).
Jul 22 '05 #5
ol*****@inspire.net.nz (Old Wolf) wrote in message
Why did you include that function? It has nothing to do with
anything else in this program because you never go
std::cout << (something with char type or convertible to char).


That's the weird part. If you take out Y::operator << (which I agree
never gets called) then I get

Success!
Success!

I'm not sure why y::operator << is hiding anything, but if it hides
one of the ::operator<<, why doesn't it hide both?

ned.
Jul 22 '05 #6

"Ned Harding" <nh******@extendthereach.com> wrote in message
news:34**************************@posting.google.c om...
ol*****@inspire.net.nz (Old Wolf) wrote in message
Why did you include that function? It has nothing to do with
anything else in this program because you never go
std::cout << (something with char type or convertible to char).


That's the weird part. If you take out Y::operator << (which I agree
never gets called) then I get

Success!
Success!

I'm not sure why y::operator << is hiding anything, but if it hides
one of the ::operator<<, why doesn't it hide both?

ned.


Try moving the operator that takes the X::Abc1* parameter INSIDE the X
namespace. Then you won't have any problem.

BTW, I have no idea how it's possible to "hide" the operator like you've
described, which I've verified with all kinds of variations of that Y::
operator. But even though it's never called, it does indeed prevent VC7
from "seeing" the global namespace operator that takes a parameter type from
another namespace. But I tend not to sweat stuff like that, once I've found
a solution! :-)

-Howard


Jul 22 '05 #7
nh******@extendthereach.com (Ned Harding) wrote:
ol*****@inspire.net.nz (Old Wolf) wrote in message
Why did you include that function? It has nothing to do with
anything else in this program because you never go
std::cout << (something with char type or convertible to char).
That's the weird part. If you take out Y::operator << (which I agree
never gets called) then I get

Success!
Success!


I don't (gcc 3.4.1). Perhaps it is a compiler bug in MSVC.
(On BCC 5.5.1, both versions give Success! Success!, I suppose it
looks in the global namespace when it should not.. BCC is not
famous for its lack of bugs)
I'm not sure why y::operator << is hiding anything, but if it hides
one of the ::operator<<, why doesn't it hide both?

Jul 22 '05 #8

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

Similar topics

2
by: Matt | last post by:
For example, we want to produce a new language called NewLang. When we implement the compiler of NewLang, we should use languages other than NewLang. Correct? But I heard Java is implemented in...
21
by: asm | last post by:
Hi All, Like typdef, does C have further support for portability? Thanks, ASM
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
22
by: larry | last post by:
I was just looking at a demo training that mindleaders has on .net training: http://www.mindleaders.com/products/democourse3.asp And I don't believe this is correct or at least is misleading...
28
by: C# Learner | last post by:
Note ---- Please use a fixed-width font to view this, such as Courier New. Problem
22
by: Matt | last post by:
Some people identify Microsoft C# is Proprietary programming language. What is Proprietary programming language then? How does it differ from other languages such as C++, or Java?? Please...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
84
by: aarklon | last post by:
Hi all, I found an interesting article here:- http://en.wikipedia.org/wiki/Criticism_of_the_C_programming_language well what do you guys think of this article....??? Is it constructive...
43
by: Adem24 | last post by:
The World Joint Programming Language Standardization Committe (WJPLSC) hereby proclaims to the people of the world that a new programming language is needed for the benefit of the whole mankind in...
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...
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...
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 projectplanning, coding, testing,...
0
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...

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.