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

  • Home
  • SEARCH
  • 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 7002069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:52:56+00:00 2026-05-27T20:52:56+00:00

While answering this question I came across an interesting difference in the overload resolution

  • 0

While answering this question I came across an interesting difference in the overload resolution of rvalue to reference between member and non-member operators.

Given two non-member operators, one passing the left parameter as const and one as non-const, both GCC 4.4.3 and MSVC2010 choose the const version when called with an rvalue.

However, given two member operators, one const and one non-const, both compilers choose the non-const version.

I assume that both compilers are complying with the standard on this, so I am curious as to why there is this discrepancy between const overload resolution between members and non-members. Please enlighten me 🙂

Here’s some code to illustrate the difference:

#include <iostream>

class C {
public:
    C(int i) { }

    /*
    C operator<<(C const &rhs) {
        std::cout << "member non-const" << std::endl;
        return *this;
    }
    C operator<<(C const &rhs) const {
        std::cout << "member const" << std::endl;
        return *this;
    }
    //*/
};

C operator<<(C &lhs, C const &rhs) {
    std::cout << "non-member non-const" << std::endl;
    return lhs;
}
C operator<<(C const &lhs, C const &rhs) {
    std::cout << "non-member const" << std::endl;
    return lhs;
}

int main() {
    // Will print:
    // "non-member const" when member operators are commented out
    // "member non-const" when members are uncommented
    C(5) << 6;
}
  • 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-27T20:52:56+00:00Added an answer on May 27, 2026 at 8:52 pm

    Rvalues cannot bind to references-to-nonconst, so only the reference-to-const overload of the free function is viable: operator<<(C(5), 6);.

    This does not apply to the member-operator, which is simply C(5).operator<<(6), and the C-object isn’t a function argument. You would have to say static_cast<const C &>(C(5)) << 6 to get the const version there, since it is the constness of this that distinguishes the two member operator overloads.

    In the presence of both member and free-function operators, the member function is preferred, so putting all this together explains the observed behaviour.

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

Sidebar

Related Questions

Kicking around some small structures while answering this post , I came across the
While answering another question I bumped into this interesting situation Where WCF is happy
While answering this question I noticed that I have never come across any property
While answering this question regarding safe escaping of filename with spaces (and potentially other
While answering this question I noticed that I got the following dialog while atempting
So, I was playing around with Python while answering this question , and I
This came up while answering BufferedWriter only works the first time As far as
While answering this question DOM parser: remove certain attributes only I noticed that some
Before answering this question, understand that I am not asking how to create my
While answering a question on SO yesterday, I noticed that if an object is

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.