Bug 7049 - Overloading "cout <<" in one namespace obscures similar functions in a different namespace
Summary: Overloading "cout <<" in one namespace obscures similar functions in a differ...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.1
: P3 normal
Target Milestone: ---
Assignee: Paolo Carlini
URL:
Keywords:
: 21924 26037 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-06-17 01:46 UTC by steev
Modified: 2006-01-30 18:17 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
bug2.cpp (224 bytes, text/x-c++ )
2003-05-21 15:16 UTC, steev
Details
testout (782 bytes, application/octet-stream)
2003-05-21 15:16 UTC, steev
Details

Note You need to log in before you can comment on or make changes to this bug.
Description steev 2002-06-17 01:46:01 UTC
If you define a operator<<(ostream&,thing) for an object X in one namespace, then in another namespace, define the same sort of function for a different object Y, it hides the original function so you can't print objects of type X in the namespace associated with object Y.

Fairly obvious workaround:  don't define operator<<'s in namespaces - make sure they always go in the global namespace.  Bjarne wouldn't be pleased, though.  I'm not 100% certain this is a bug and not a "feature" of C++ - it just looks suspicious.

I haven't been able to find this in the list of known bugs, but it doesn't mean it's not there.

Thanks for your time, Steev

Release:
3.1 (sparc-sun-solaris2.8)

Environment:
Solaris 8

How-To-Repeat:
Compile bug2.cpp with "g++ -c bug2.cpp".  It'll fail in the way indicated in file "testout".  If you place the definition of the operator<< on Y in the global namespace, g++ can suddenly find the definition of operator<< on X.
Comment 1 steev 2002-06-17 01:46:01 UTC
Fix:
For workaround, see above.
Comment 2 Paolo Carlini 2002-06-17 02:13:16 UTC
State-Changed-From-To: open->feedback
State-Changed-Why: Hi. For some reason, both 'bug2.cpp' and 'testout' got lost.
    Could you possibly provide them again?
    Thanks, Paolo.
Comment 3 Paolo Carlini 2002-06-17 05:07:47 UTC
State-Changed-From-To: feedback->open
State-Changed-Why: Attachments provided.
Comment 4 Paolo Carlini 2002-06-18 05:23:37 UTC
Responsible-Changed-From-To: unassigned->paolo
Responsible-Changed-Why: .
Comment 5 Paolo Carlini 2002-06-18 05:23:37 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Philip is right (also double checked with two other respected
    compilers).
Comment 6 philip 2002-06-18 12:30:14 UTC
From: Philip Martin <philip@codematters.co.uk>
To: steev@paradigmds.com, gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: c++/7049 Overloading "cout <<" in one namespace obscures similar functions in a different namespace
Date: 18 Jun 2002 12:30:14 +0100

 This is not a bug, that's how name lookup works.
 
 The compiler will lookup operator<< in the anonymous namespace where
 the statement "cout << x" occurs, the std:: namespace where ostream is
 defined, and the ns:: namespace where class X is defined.  Since the
 compiler will find an operator<< by doing this it won't look anywhere
 else.  In particular operator<< in the global namespace will not be
 considered.
 
 Put operator<< for X where it belongs, in the ns:: namespace that
 defines class X.
 
 -- 
 Philip
Comment 7 Paolo Carlini 2006-01-30 18:00:10 UTC
*** Bug 26037 has been marked as a duplicate of this bug. ***
Comment 8 Paolo Carlini 2006-01-30 18:00:39 UTC
Reopen to adjust to...
Comment 9 Paolo Carlini 2006-01-30 18:01:10 UTC
... invalid.
Comment 10 Andrew Pinski 2006-01-30 18:17:20 UTC
*** Bug 21924 has been marked as a duplicate of this bug. ***