Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:43:35+00:00 2026-06-07T04:43:35+00:00

I found a strange behaviour of C++ resolution of operator-overloading, I can’t explain myself.

  • 0

I found a strange behaviour of C++ resolution of operator-overloading, I can’t explain myself. A pointer to some resource describing it would be just as nice as an answer.

I have 2 translation units. In one (called util.cpp/h) I declare and define two operators (I omit the real implementations for readabilty, the problam occurs anyway):

// util.h
#ifndef GUARD_UTIL
#define GUARD_UTIL

#include <iostream>

std::istream& operator>>(std::istream& is, const char* str);
std::istream& operator>>(std::istream& is, char* str);
#endif

And:

//util.cpp
#include "util.h"
#include <iostream>

std::istream& operator>>(std::istream& is, const char* str) {
  return is;  
}
std::istream& operator>>(std::istream& is, char* str) {
  return is;  
}

These operators are, if course in global namespace, since they operate on std types and built-in types and should be usable from everywhere. They just work fine from global namespace (e.g. from main()) or with explicitly telling the compiler that they are in global namespace (see code example).

In another translation unit (called test.cpp/h) I use these operators within a namespace. This works until I put a similar operator into this namespace. As soon as this operator is added, the compiler (e.g. gcc or clang) is not able to find a viable operator>> anymore.

// test.h
#ifndef GUARD_TEST
#define GUARD_TEST

#include <iostream>

namespace Namespace {
  class SomeClass {   
    public:
      void test(std::istream& is);
  };

  // without the following line everything compiles just fine
  std::istream& operator>>(std::istream& is, SomeClass& obj) { return is; }; 
}

#endif

And:

//test.cpp
#include "test.h"
#include "util.h"
#include <iostream>

void Namespace::SomeClass::test(std::istream& is) {
  ::operator>>(is, "c"); //works
  is >> "c" //fails
}

Why does the compiler find the correct operator when there is no operator>> in Namespace but fails to find when there is one? Why does the operator affect the ability of the compiler to find the correct one even if it has a different signature?

One attempt to fix this was to put

std::istream& operator>>(std::istream& is, const char* str) { ::operator>>(is, str); }

into Namespace, but than the linker complains about previous definitions. So additional: Why can the linker find something the compiler doesn’t find?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:43:37+00:00Added an answer on June 7, 2026 at 4:43 am

    This is a name hiding issue. The standard says (c++03, 3.3.7/1)

    A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived
    class (10.2).

    The “name” on your case would be operator>> and namespaces constitute nested declarative regions.

    The easiest way to fix that would be to use a using declaration where you declare the namespace-local operator<<:

    namespace your_namespece {
        std::istream& operator>>(std::istream& is, SomeClass& obj) { return is; }; 
        using ::operator>>;
    }
    

    Note that this feature doesn’t interfere with Koenig lookup (at least in your case, in principle, it can), so IO operators from std:: will still be found.

    PS: Another possibility for working aroud this issue would be defining the operator for SomeClass as an inline friend. Such functions are declared at the namespace level (outside of “their” class), but are not visible from there. They can only be found by Koenig lookup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found out some strange behaviour that I can't explain in my iOS App.
I'm hacking my way through learning Flex and have found some strange behaviour. When
I've found some strange behaviour of firefox 3.6 - when I call HtmlPage.Window.Confirm() method
I found strange generics behaviour. In two words - thing I realy want is
I just found some strange behavior of database's order by clause. In string comparison,
I was experimenting with toCharArray() and found some strange behavior. Suppose private static final
I'm working on a heavily data-bound Win.Forms application where I've found some strange behavior.
I've tried to learn the signal handling in C, when found strange behaviour. When
I experienced very strange behaviour with a CakePHP site in production. Some views did
I found a strange behaviour in an ant build of a git repository. Here's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.