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 3628112
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:58:28+00:00 2026-05-18T23:58:28+00:00

While writing a test suite, I needed to provide an implementation of operator<<(std::ostream&… for

  • 0

While writing a test suite, I needed to provide an implementation of operator<<(std::ostream&... for Boost unit test to use.

This worked:

namespace theseus { namespace core {
    std::ostream& operator<<(std::ostream& ss, const PixelRGB& p) {
        return (ss << "PixelRGB(" << (int)p.r << "," << (int)p.g << "," << (int)p.b << ")");
    }
}}

This didn’t:

std::ostream& operator<<(std::ostream& ss, const theseus::core::PixelRGB& p) {
    return (ss << "PixelRGB(" << (int)p.r << "," << (int)p.g << "," << (int)p.b << ")");
}

Apparently, the second wasn’t included in the candidate matches when g++ tried to resolve the use of the operator. Why (what rule causes this)?

The code calling operator<< is deep within the Boost unit test framework, but here’s the test code:

BOOST_AUTO_TEST_SUITE(core_image)

BOOST_AUTO_TEST_CASE(test_output) {
    using namespace theseus::core;
    BOOST_TEST_MESSAGE(PixelRGB(5,5,5)); // only compiles with operator<< definition inside theseus::core
    std::cout << PixelRGB(5,5,5) << "\n"; // works with either definition
    BOOST_CHECK(true); // prevent no-assertion error
}

BOOST_AUTO_TEST_SUITE_END()

For reference, I’m using g++ 4.4 (though for the moment I’m assuming this behaviour is standards-conformant).

  • 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-05-18T23:58:29+00:00Added an answer on May 18, 2026 at 11:58 pm

    In argument dependent lookup (the correct name for koenig lookup) the compiler adds to the overloaded function set the functions which are declared in the namespaces of each parameter.

    In your case, the first operator<< is declared in the namespace thesus::core, which is the type of the argument you call the operator with. Therefore this operator<< is considered for ADL because it’s declared in an associated namespace

    In the second case, the operator<< seems to be declared in the global namespace which is not an associated namespace as parameter one is of type from namespace std and param 2 is of type from namespace theseus::core.

    Actually, probably your 2nd operator<< isn’t declared in global namespace as that would be found through looking in parent scopes.. maybe you’ve got something more like this? If you can post more code we can give a better answer.


    Ok I remembered, ADL doesn’t lookup in parent scopes when it finds a name in the current scope. So the boost macro BOOST_TEST_MESSAGE expands to include an operator<< and there is some in the scope tree a non-viable operator<< between the expression and global scope. I updated code to illustrate this (hopefully).

    #include <iostream>
    
    namespace NS1
    {
      class A
      {};
    
      // this is found by expr in NS2 because of ADL
      std::ostream & operator<<(std::ostream &, NS1::A &);
    }
    
    
    // this is not seen because lookup for the expression in NS2::foo stops when it finds the operator<< in NS2
    std::ostream & operator<<(std::ostream &, NS1::A &);
    
    namespace NS2
    {
        class B
        {};
    
        // if you comment this out lookup will look in the parent scope
        std::ostream & operator<<(std::ostream &, B &);
    
        void foo(NS1::A &a)
        {
            std::cout << a;
        }  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to update my SQL lite database with the native update-method of
I have a new web app that is packaged as a WAR as part
I am using a 3rd-party rotator object, which is providing a smooth, random rotation

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.